Reputation: 3
Hi I am new to bash and using sed need a little help
I have two txt files i need to copy and paste between them the first file I know what the text is and placed of the text but the second txt file I don't know the text but I do know the placed of the text is.
In file1 put the two text words or numbers from file2 and place them like I show below.
When I create file2 all I am going to know about it will have two words or numbers on the same line4
I have been trying with this
sed $'10{e sed "4!d" /home/Desktop/file1.txt\n;d}' /home/Desktop/file2.txt
and
awk 'NR==4{a=$0}NR==FNR{next}FNR==10{print a}4' /home/Desktop/file2.txt /home/Desktop/file1.txt
This is what my files would look like
file1.txt
cat
hat
sat
fat
mat
rat
file2.txt
line1
line2
line3
text1 text2
line5
I need it to look like this
file1.txt
cat
hat
sat text1
fat text2
mat
rat
thanks for any help
Upvotes: 0
Views: 214
Reputation: 58371
This might work for you (GNU sed):
sed -E '1{x;s#^#sed -n 4p file2#e;x};3{G;s/\n(\S+).*/ \1/};4{G;s/\n\S+//}' file1
Stuff the line from file2 into the hold space when processing file1 and append and manipulate that line when needed.
A more explicit explanation:
By default, sed reads each line of a file. For each cycle, it removes the newline, places the result in the pattern space, goes through a sequence of commands, re-appends the newline and prints the result e.g. sed '' file
replicates the cat
command. The sed commands are usually placed between '...'
and represent a cycle, thus:
1{x;s#^#sed -n 4p file2#e;x}
1{..}
executes the commands between the ellipses on the first line of file1. Commands are separated by ;
'sx
sed provides two buffers. After removing the newline that delimits each line of a file, the result is placed in the pattern space. Another buffer is provided empty, at the start of each invocation, called the hold space. The x
swaps the pattern space for the hold space.s#^#sed -n 4p file2#e
this inserts another sed invocation into the empty hold space and evaluates it by the use of the e
flag. The second invocation turns off implicit printing (-n
option) and then prints line 4 of file2 only.x
the hold space is now swapped with the pattern space.Thus, line 4 of file2 is placed in the hold space.3{G;s/\n(\S+).*/ \1/}
3{..}
executes the commands between the ellipses on the third line of file1.G
append the contents of hold space to the pattern space using a newline as a separator.s/\n(\S+).*/ \1/
match on the appended hold space and replace it by a space and the first column.4{G;s/\n\S+//}
4{..}
executes the commands between the ellipses on the fourth line of file1.G
append the contents of hold space to the pattern space using a newline as a separator.s/\n\S+//
match on the appended hold space and remove the newline and the first column, thus leaving a space and the second column.
mUpvotes: 1
Reputation: 22012
Assuming you want to append the fields of the 4th line of file2.txt to the 3rd and the following lines of file1.txt, how about:
awk 'FNR==NR {if (FNR==4) split($0, ary, " "); next} {print $0 " " ary[FNR - 3 + 1]}' /home/Desktop/file2.txt /home/Desktop/file1.txt
Result:
cat
hat
sat text1
fat text2
mat
rat
Upvotes: 1