I have 2 files: number1.txt and number2.txt.
100 Jan
223 January
621 Jan
589 January
I want to join both files to single file each line so the result file like this:
100 Jan
621 Jan
223 January
589 January
I try to run the bash:
#!/bin/bash
number=`cat number2.txt | awk '{print $2}' | sort | wc -l`
awk '{print $2}' number2.txt > month.txt
for i in $(seq 1 $number);
do
user=`sed -n "$i p" month.txt`
cat number1.txt | grep "^$user$" > result$i.txt
cat number2.txt | grep "^$user$" >> result$i.txt
done
but the result files is empty file. I think the problem in the code grep "^$user$" but I dont know how to solve it.
If I change to grep "$user" , The result is not what I wanted and the result are like this:
100 Jan
223 January
621 Jan
589 January
223 January
589 January
Please help me to solve my problem.
Your usage of ^
and $
means that the line must consist only of $user
, nothing else. If you want the lines ending in $user
, do a
grep " $user$"
instead.
Single grep
will give you desired output:
$ grep -hv 'Jan$' number?.txt
223 January
589 January
$ grep -hv 'January$' number?.txt
100 Jan
621 Jan
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.