I have a file, file1.csv
containing:
This
is
some
text.
I am using while read line
to cycle through each line, e.g.:
while read line; do
echo $line
done < file1.csv
I have another file, with an identical number of lines, called file2.csv
:
A
B
C
D
The data each line corresponds to data in the first file of the same line number.
file2.csv
?Fortunately, using a similar for loop, you can insert text into each file with a single command. Execute the following command to insert the file's name, followed by a newline, followed by the text Loops Rule! into each file: for FILE in *; do echo -e "$FILE\nLoops Rule\!" > $FILE; done.
Comparison Using cmp GNU cmp compares two files byte by byte and prints the location of the first difference. We can pass the -s flag to find out if the files have the same content. Since the contents of file1 and file2 are different, cmp exited with status 1.
Use comm -12 file1 file2 to get common lines in both files. You may also needs your file to be sorted to comm to work as expected. Or using grep command you need to add -x option to match the whole line as a matching pattern. The F option is telling grep that match pattern as a string not a regex match.
You could try with the paste
utility:
$ cat one
this
is
some
text
$ cat two
1
2
3
4
$ while read a b ; do echo $a -- $b ; done < <(paste one two)
this -- 1
is -- 2
some -- 3
text -- 4
You can use the paste
command:
$ paste -d, file{1,2}.csv | while IFS=, read x y; do echo "$x:$y"; done
This:A
is:B
some:C
text.:D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With