Output of sed command to another sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output of sed command to another sed command
# 8  
Old 12-05-2013
Thank you bakunin for your comprehensive post. I will explore this option and hopefully create a cleaner solution

---------- Post updated at 12:37 PM ---------- Previous update was at 12:30 PM ----------

Quote:
Originally Posted by hergp
Hmm, it works here (without the -i option) on CentOS 6.4:
Code:
$ cat file1
head1
head2
head3
head4
$ cat file2
line1
line2
line3
$ sed '1i\'<(sed -n 1,2p file1) file2
head1
head2
line1
line2
line3

When I do the same I get the following

Code:
 
$ cat file1
head1
head2
head3
head4
$ cat file2
tail1
tail2
tail3
tail4
$ sed '1i\'<(sed -n 1,2p file1) file2
/dev/fd/63
tail1
tail2
tail3
tail4

Smilie
# 9  
Old 12-05-2013
Have you tried with the space, as RudiC suggested?

Code:
$ sed '1i\'<(sed -n 1,2p file1) file2
/dev/fd/63
tail1
tail2
tail3
tail4
$ sed '1i\' <(sed -n 1,2p file1) file2
head1
head2
tail1
tail2
tail3
tail4

(bash 4.2.24 on Debian)
# 10  
Old 12-05-2013
Quote:
Originally Posted by CarloM
Have you tried with the space, as RudiC suggested?

Code:
$ sed '1i\'<(sed -n 1,2p file1) file2
/dev/fd/63
tail1
tail2
tail3
tail4
$ sed '1i\' <(sed -n 1,2p file1) file2
head1
head2
tail1
tail2
tail3
tail4

(bash 4.2.24 on Debian)
Did try this also:

Code:
 
$ cat file1
head1
head2
head3
head4
$ cat file2
tail1
tail2
tail3
tail4
$ sed '1i\' <(sed -n 1,2p file1) file2
sed: couldn't write -1 items to stdout: Success
$ cat file2
tail1
tail2
tail3
tail4

# 11  
Old 12-05-2013
I dug into this a little bit more, and it seems, it depends on your shell, if you need a blank as RudiC suggested or not.

First with bash:
Code:
$ bash -c "sed '1i\' <(sed -n 1,2p file1) file2"
head1
head2
line1
line2
line3
$ bash -c "sed '1i\'<(sed -n 1,2p file1) file2"
/dev/fd/63
line1
line2
line3

Without the blank you get the line with /dev/fd/nnn as BigCroyd pointed out.

Whereas ksh does not seem to care
Code:
$ ksh -c "sed '1i\' <(sed -n 1,2p file1) file2"
head1
head2
line1
line2
line3
$ ksh -c "sed '1i\'<(sed -n 1,2p file1) file2"
head1
head2
line1
line2
line3

---------- Post updated at 14:22 ---------- Previous update was at 14:18 ----------

Quote:
Originally Posted by BigCroyd
Code:
$ sed '1i\' <(sed -n 1,2p file1) file2
sed: couldn't write -1 items to stdout: Success

... and another version... Smilie

This syntax does not seem to be very advisable with all these different outcomes. I withdraw my original example.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Command Output

I want to know how this command output will be sed 's/^"\(.*\)"$/\1/' <filename> (3 Replies)
Discussion started by: abhilashnair
3 Replies

2. Shell Programming and Scripting

sed command is saving output as blank file

Hi, I am working on a script where I am adding adding colors to few of the info in the output. Now , after that is done , I see colour codes in log files which I don't want to see.:mad::mad::mad::mad: So , I tried using sed command in script as below which gives me o/p (new.log) as blank file... (7 Replies)
Discussion started by: Dream4649
7 Replies

3. Shell Programming and Scripting

sed command not getting the right output

Hello. Ive spent probably a hour on this problem, and cant figure it out. Let me explain the problem. I run head -n5 datasets/q13data.txt and get this : $$001011<-:::$$<-::: ' GreenWHITE<-::3.1415<-:::"BLACK fubar<-:::phi<-:::foochi $$$Yellow->:::'<-:::VOIDTue taochi->::$->:::Blue"... (12 Replies)
Discussion started by: jozo95
12 Replies

4. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. Shell Programming and Scripting

How to append a value to the output after using sed command?

Hi All, I have a file where I am converting newlines to comma separated values but I would like to append zero if the output is empty Here is the command I am using sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' test1.txt test1.txt will have comma seperated values but sometimes this file can be... (6 Replies)
Discussion started by: rajeevm
6 Replies

6. Shell Programming and Scripting

Abnormality while piping tr command output to sed

i have a file seperated each line seperated by newline. For example alpha beta gamma i am trying to replace the newlines to "," but dont want , present at the end of the line so i am trying the below one liner . but not sure whats wrong but its not working cat myfile | tr -s '\n' ',' | sed... (9 Replies)
Discussion started by: chidori
9 Replies

7. Shell Programming and Scripting

Help needed with file output awk sed command - please

Hi I have a file that contains lines starting with a particular string plus a Colon: I need to output all these lines but only what comes after the colon Can you pelase assist? Example of lines in the file: com.ubs.f35.cashequities/cashequities: 1 2 ... (5 Replies)
Discussion started by: mnassiri
5 Replies

8. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

9. UNIX for Dummies Questions & Answers

Using grep output as input for sed command

Hi, I would like to know if this is possible, and if so what can i do to make this work. I would like to grep a line X from fileA and then use the output to replace a word Y in fileB. grep "line X" fileA | sed -e 's/Y/X/g' > outfile this statement does not work, as i do not know how to... (7 Replies)
Discussion started by: cavanac2
7 Replies

10. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies
Login or Register to Ask a Question