I want to print the output of file1 to first column in new file and file 2 to the second column in the new file.
Something like this.
file1
AAA
BBB
CCC
file2
XXX
YYY
ZZZ
file3
AAA XXX
BBB YYY
CCC ZZZ
paste command will do this job out-of-the-box:
paste file1 file2 > file3
AAA XXX
BBB YYY
CCC ZZZ
Try this click here
You can use paste and format using cut to remove leading and trailing spaces
Related
Using awk or sed, how to get contents between two parameters , parameters occurs multiple time in a file
For instance, file contents
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
16
Leaving BBB
Currently I am using
cat 1.txt |sed -n '/Entering AAA/,/Leaving AAA/ p'
with this , I am getting contents between first occurrence of "Entering AAA" and first occurrence of "Leaving AAA"
ie
Entering AAA
12
Entering BBB
13
Leaving AAA
But , I want contents from first occurrence of "Entering AAA" to last occurrence of "Leaving AAA"
Expected output :
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
Kindly help.
In any awk using a 2-pass approach:
$ awk 'NR==FNR{if (/Leaving AAA/) end=NR; next} /Entering AAA/{f=1} f; FNR==end{exit}' file file
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
Alternatively doing it in one pass with GNU awk for multi-char RS and RT:
$ awk -v RS='Entering AAA.*Leaving AAA' 'RT{print RT}' file
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
Short tac + awk trick:
tac file | awk '/Leaving AAA/,/Entering AAA/' | tac
The output:
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
Here is an alternative solution using perl to get this done in a single pass in slurp mode:
perl -0777 -pe 's/(?ms).*?(^Entering AAA.*Leaving AAA\R*).*/$1/' file
Entering AAA
12
Entering BBB
13
Leaving AAA
14
Leaving AAA
15
Leaving AAA
.* is a greedy pattern that ensures to match longest string between start and end patterns.
(?ms) enables MULTILINE and DOTALL modes for this regex
You may also use a back-reference:
perl -0777 -pe 's/(?ms).*?(^Entering (AAA).*Leaving \2\R*).*/$1/' file
I'm trying to parse a log file that will have lines like this:
aaa bbb ccc: [DDD] efg oi
aaa bbb ccc: lll [DDD] efg oo
aaa bbb ccc: [DDD]
where [DDD] can be at any place in line.
Only one thing will be between [ and ] in any line
Using awk and space as a delimiter, how can I print 1st, 3rd and all data (whole string) between [ and ]?
Expected output: aaa ccc: DDD
gawk(GNU awk) approach:
Let's say we a file with the following line:
aaa bbb ccc: ddd [fff] ggg hhh
The command:
awk '{match($0,/\[([^]]+)\]/, a); print $1,$3,a[1]}' file
The output:
aaa ccc: fff
match(string, regexp [, array]) Search string for the longest, leftmost substring matched by the regular expression regexp and return the character position (index) at which that substring begins (one, if it starts at the beginning of string). If no match is found, return zero..
Given:
$ cat file
aaa bbb ccc: [DDD] efg oi
aaa bbb [ccc:] lll DDD efg oo
aaa [bbb] ccc: DDD
(note -- changed from the OP's example)
In POSIX awk:
awk 'BEGIN{fields[1]; fields[3]}
{s=""
for (i=1;i<=NF;i++)
if ($i~/^\[/ || i in fields)
s=i>1 ? s OFS $i : $i
gsub(/\[|\]/,"",s)
print s
}' file
Prints:
aaa ccc: DDD
aaa ccc:
aaa bbb ccc:
This does not print the field twice if it is both enclosed in [] and in the selected fields array. (i.e., [aaa] bbb ccc: does not print aaa twice) It will also print in correct field order if you have aaa [bbb] ccc ...
awk '$5=="[DDD]"{gsub("[\\[\\]]","");print $1,$3,$5}' file
or
awk '$5=="[DDD]"{print $1,$3, substr($5,2,3)}' file
aaa ccc: DDD
i have a text file which contains some specified strings, firstly locate them , delete few lines between them , then add something new. .e.g
$cat foo.txt
aaaaaa
bbbbbb
####start
123456
987655
121212
####end
cccccc
dddddd
$ cat bar.txt
AA
BB
CC
now, i want a bash script to edit foo.txt. delete content between "####start" and "####end" , insert content of bar.txt
new foo.txt will be
$cat foo.txt
aaaaaa
bbbbbb
####start
AA
BB
CC
####end
cccccc
dddddd
We can execute the following command,
sed -i '/####start/,/####end/{//!d};/####start/r bar.txt' foo.txt
The first part "/####start/,/####end/{//!d}" is to remove the lines in between the pattern and second part "/####start/r bar.txt" is to add the contents of bar.txt to foo.txt.
**Test:**
✓ krishna-VB# cat foo.txt
aaaaa
bbbbbb
####start
123465
454687
146546
####end
cccccc
dddddd
✓ krishna-VB# cat bar.txt
AA
BB
CC
✓ krishna-VB# sed -i '/####start/,/####end/{//!d};/####start/r bar.txt' foo.txt
✓ krishna-VB# cat foo.txt
aaaaa
bbbbbb
####start
AA
BB
CC
####end
cccccc
dddddd
If you want to verify the just output before modification of file, you can remove '-i' option before sed.
This awk one-liner should help:
awk 'NR==FNR{a[NR]=$0;n=NR;next}
!p;/##start/{p=1;for(i=1;i<=n;i++)print a[i];}/#end/{p=0;print}' bar foo
I have 2 files:
File1 --------------------------------------->File2
abc -----------------------------------------> abc
cde -----------------------------------------> cde,xyz,efg,hij,...,n
efg -----------------------------------------> lmn,opq,weq,...n
Now I want to File1 line1 -> File2 line1, line 2 -> line2 and so on...
However, in file2 a single line can have multiple entries separated with 'comma'.
now if the entry in file1 matches with the any of the corresponding line entry in file 2 -> result ok
Else show the diff...
For example:
FILE1 ---------------------- FILE2
cde ---------------------- cde,xyz,efg,hij,opt
the result should be ok because cde exist in both files.
Can you please help me out to write a shell script for the same
sdiff gave me the entries difference also
Consider these two test files:
$ cat file1
abc
cde
efg
$ cat file2
abc
cde,xyz,efg,hij,n
lmn,opq,weq,n
Consider the command:
$ awk -F, 'FNR==NR{a[NR]=$1;next} {f=0;for (i=1;i<=NF;i++)if($i==a[FNR])f=1;if(f)print "OK";else print a[FNR]" -----> " $0}' file1 file2
OK
OK
efg -----> lmn,opq,weq,n
This prints OK on every line for which the key in file1 is found anywhere on the corresponding line in file2. If it is not, it prints both lines as shown.
Another example
From the comments, consider these two files in which all lines have a match:
$ cat f1
abc
cde
mno
$ cat f2
abc
efg,cde,hkl
mno
$ awk -F, 'FNR==NR{a[NR]=$1;next} {f=0;for (i=1;i<=NF;i++)if($i==a[FNR])f=1;if(f)print "OK";else print a[FNR]" -----> " $0}' f1 f2
OK
OK
OK
I want to find a line in a txt file and then insert string 3 lines above the found line
Input:
aaa
bbb
ccc
ddd
eee
fff
I want to look for "eee" and then print "WWW" 3 lines above it. Output:
aaa
WWW
bbb
ccc
ddd
eee
fff
I'm using awk and can only print "WWW" 1 line above "eee", and not 3:
awk '/eee/{print "WWW"} 4' file.txt
any ideas?
One way:
awk '{a[NR]=$0;}/eee/{a[NR-3]="www\n" a[NR-3];}END{for(i=1;i<=NR;i++)print a[i];}' file