join/merge 2 files using just sed - sed

using just sed
i want to merge file 1 and 2 so it looks like file 3
with sed please if poss
file 1
bob
rob
kate
fred
mike
file 2
http://www.somesite/play/episodes
http://www.somesite/play/episodes
http://www.somesite/play/episodes
http://www.somesite/play/episodes
http://www.somesite/play/episodes
file 3
bob
http://www.somesite/play/episodes
rob
http://www.somesite/play/episodes
kate
http://www.somesite/play/episodes
fred
http://www.somesite/play/episodes
mike
http://www.somesite/play/episodes

Solution 1st: In sed:
sed 'R FiLe2' FiLe1
Solution 2nd: With simple paste command.
paste -d"\n" Input_FiLe1 Input_FiLe2
Solution 3rd: Adding awk command too now into solution:
awk 'FNR==NR{a[FNR]=$0;next} {print a[FNR] RS $0}' FiLe1 FiLe2
Output will be as follows for both the above codes.
bob
http://www.somesite/play/episodes
rob
http://www.somesite/play/episodes
kate
http://www.somesite/play/episodes
fred
http://www.somesite/play/episodes
mike
http://www.somesite/play/episodes
You could take this command's output into file3 by concatenating > file3 in case you need it.

Related

extract some values from file#1 and others from file#2 and print them into file#3

I hope you can help with this question, I have two files, each one has some lines that I need in a third file. But I need to take some entire lines (with values in 5 or 6 columns) from file#1 and others from file#2 and save them in file#3 (keeping the line number). Example:
File 1
1. mike
2. linda
3. matt
4. eric
5. emma
File 2
1. beth
2. shelly
3. michael
4. andy
5. theo
File 3 (output)
1. mike
2. shelly
3. matt
4. andy
5. emma
So, I need to extract the values of line 2 and 4 (from file#2) and print them in a third file while keeping the content of lines 1, 3 and 5 from file#1.
I tried this using sed (easy example):
sed -n -e 1,3p -e 5p file1.txt > file3.txt
This will take lines 1,3 and 5 from my file#1 and print them in file#3, but I don't know how to get the lines from file#2 (2 and 4) and add them into file#3.
Using grep to annotate with file names:
grep -H '.*' in1 in2 | sed '/in1:[24]/d;/in2:[135]/d;s/[^:]*://' | sort
Output:
1. mike
2. shelly
3. matt
4. andy
5. emma
sed probably isn't a very suitable tool for this. How about
paste in1 in2 | awk -F '\t' '{ print $(1+(1+NR)%2) }'
The Awk variable NR is the current input line number and the modulo operator NR%2 flip-flops between 1 and 0. We need to perform a couple of additions to get it to flip-flop between 1 and 2. Then it's easy to print alternating columns from the paste output.

Finding all the names with a given surname in a file using sed

For the given file with following input test, I want to out put all the names with surname Smith, I have done it using grep:
grep -o -w "[A-Za-z]* Smith" filename
But I want to know how it can be done using sed, I have tried
sed -e 's/[A-Za-z]* Smith/&/g' filename
but it is printing the entire line.
Input files text :
John Smith Kent Smith Adam Smith
Adam Jones Devlin Thomas Bill Kate
Mark Taylor Dean Bush Kane King Nicole Smith
John Williams Adam Cole
James Brown Jason Taylor Mark Rose
Rache Davies Christian Williams
Chris Evans Steve Williams Craig Thomas Jack Smith
Jonna Wilson Jack Jones Jason Patt
Chris Thomas Connor Smith
Kat Watson Kat Smith Julia Roberts Greg Smith Bill Smith
Michael Johnson
Try this:
sed '/[A-Za-z]* Smith/!d;s//\n&\n/;s/^[^\n]*\n//;P;D' file
Explanations:
/[A-Za-z]* Smith/!d: retains only lines containing letters followed by Smith
s//\n&\n/: adds a newline(\n) before and after first string matching above address pattern
s/^[^\n]*\n//: removes unwanted strings(those not starting with \n)
lines now only contains desired names with surrounding \n character on the first match we can loop on with multiline P and D commands that act as a loop on strings containing \n characters
P prints the first part of the pattern space, up to the previously added newline character
after printing, D deletes the same first part of the pattern space
For more about P and D that are part of the multiline commands, please read http://www.grymoire.com/Unix/Sed.html.
But grep is definitely more suited for this job.
Actually, this is not as easy as I thought it was, since grep -o shows each match on a separate line. If you want to use sed twice, you can say:
sed -e 's/\([A-Za-z]* Smith\)/\n\1/g' names | sed '/Smith/!d'
you didn't ask but...
awk to the rescue!
$ awk -v sur="Smith" '{for(i=2;i<=NF;i++) if($i==sur) print $(i-1),sur}'
file
John Smith
Kent Smith
Adam Smith
Nicole Smith
Jack Smith
Connor Smith
Kat Smith
Greg Smith
Bill Smith

replacing single character in sed

I currently have a list of grades and Im trying to change all of the grades to A's, I cant work out how to use the sed to only change the grades as it keeps changing the first characters and things like that. I tried sed /s/.$/A/' but that only adds an A at the end of the string.
List:
> Steve maths A
> Steve english C
> Steve physics E
> Jane maths A
> Jane English B
> Jane physics F
>
Thanks
You could try the below. I used [A-Z]$ so that it won't change the > symbol in the last line to A . Think there may be some spaces present after the grade. In that case, you could use sed 's/[A-Z] *$/A/' file
$ sed 's/[A-Z]$/A/' file
> Steve maths A
> Steve english A
> Steve physics A
> Jane maths A
> Jane English A
> Jane physics A
>
It's not sed but this awk may do, it replaces last field with an A
awk '{$NF="A"}1' file
> Steve maths A
> Steve english A
> Steve physics A
> Jane maths A
> Jane English A
> Jane physics A
You may need to run dos2unix or similar on your file first but then:
$ sed 's/[[:upper:]][[:space:]]*$/A/' file
> Steve maths A
> Steve english A
> Steve physics A
> Jane maths A
> Jane English A
> Jane physics A
>
will work.

sed + count words on field 3

I used the following awk in order to count all words that appears in field 4
awk '{print $4}' file | awk '{print NF}' | grep -c 1
How we can to the same in sed?
Example of file:
1 2 3 4
1 2
1 2 3 4 5
1 2
1 2 3
1 2 3 4
From file sed should return the results 3 (three words on field 4)
yael
First of all, your awk is quite inefficient. Try this:
awk '$4{c++}END{print c}' file
Why do you want it in sed, BTW? This is what awk does well. If you really want it in sed, I guess something like this:
sed '/^\s*\S*\s*\S*\s*\S*\s*$/d' file | wc -l
awk explanation: In every line where fourth field is non-null, increment c. At the end, print c.
sed explanation: delete each line which matches the regexp. Then with wc count the lines of the sed output. The regexp basically says there can be maximum of two whitespace groups in the line, not counting initial and final ones, which then means there can be at most 3 fields in the line.
cut can also be used:
cut -f 5 -d' ' file | wc -w
Select the 5. column (the first one is empty due to the leading blank). The delimiter is a blank.
This might work for you:
sed 's/ *[^ ]*/&/4;t;d;' file | sed -n '$='

repeat string in a line using sed

I would like to repeat each line's content of a file, any quick solution using sed.
supposed the input file is
abc def 123
The expected ouput is:
abcabc defdef 123123
sed 's \(.*\) \1\1 ' infile
This might work for you:
echo -e 'aaA\nbbB\nccC' | sed 's/.*/&&/'
aaAaaA
bbBbbB
ccCccC
sed 'h;G;s/\n//' file.txt
It's even simpler if you take advantage of the variable \0, which holds the string that was matched:
sed 's/.*/\0\0/'
Try this
awk '{print $0$0}' temp.txt