unix - Appending multiple files into one file -
i append multiple data files single data file using cat
command. how can assign single file value new file?
i using command:
cat file1 file2 file3 > newfile.txt anotherfile=`cat newfile.txt` sort $anotherfile | uniq -c
it showing error can not open anotherfile how assign newfile value file?
original answer original question
well, easiest way cp
:
cat file1 file2 file3 > newfile.txt cp newfile.txt anotherfile.txt
failing that, can use:
cat file1 file2 file3 > newfile.txt anotherfile=$(cat newfile.txt) echo "$anotherfile" > anotherfile.txt
revised answer revised question
the original question had echo "$anotherfile"
third line; revised question has sort $anotherfile | uniq -c
third line.
assuming sort $anotherfile
not sorting contents of files mentioned in list created concatenating original files (that is, assuming file1
, file2
, file3
not contain lists of file names), objective sort , count lines found in source files.
the whole job can done in single command line:
cat file1 file2 file3 | tee newfile.txt | sort | uniq -c
or (more usually):
cat file1 file2 file3 | tee newfile.txt | sort | uniq -c | sort -n
which lists lines in increasing order of frequency.
if want sort contents of files listed in file1
, file2
, file3
list contents of each file once, then:
cat file1 file2 file3 | tee newfile.txt | sort -u | xargs sort | sort | uniq -c
it looks weird having 3 sort-related commands in row, there justification each step. sort -u
ensures each file name listed once. xargs sort
converts list of file names on standard input list of file names on sort
command line. output of sorted data each batch of files xargs
produces. if there few files xargs
doesn't need run sort
more once, following plain sort
redundant. however, if xargs
has run sort
more once, final sort has deal fact first lines second batch produced xargs sort
come before last lines produced first batch produced xargs sort
.
this becomes judgement call based on knowledge of data in original files. if files small enough xargs
won't need run multiple sort
commands, omit final sort
. heuristic "if sum of sizes of source files smaller maximum command line argument list, don't include sort".
Comments
Post a Comment