I have a sed command that is capturing a single line with sometext. The line in the file it is capturing ends with a linefeed. I am trying to utilize this variable in a pipeline, however, when I attempt to echo, or use it with other commands requiring an input, the result is a blank. Ex:
sed '1,1!d' somefile.txt | echo "$1", I know the variable itself is not empty as I can replace echo "$1" with cat $1 and see the correct printout.
edit - I have tried piping to a tr -d and removing the newline. I have confirmed the newline character is gone, yet echos still show blank. Cats do not.
edit 2 - I piped the variable into an if statement ... | if [[ -z $1 ]]; then cat $1; fi it hits the if, is determined to be empty, so runs the cat, which prints a non-empty line to console. If the variable is empty why is cat still printing out information?
What is causing this inconsistency and how can I solve my problem? The ultimate goal is to run the output of one sed, through another to replace specific lines in a target file.
sed '1,1!d' somefile.txt | sed '2,1s/.*/'$1'/' targetfile.txt
Contents of somefile.txt:
these
are
words
Contents of targetfile.txt:
The next line should say these
This line should say these
The previous line should say these
Output of echo after sed:
<empty>
Output of cat after sed:
these
Output of 2nd sed, using input from 1st:
The next line should say these
the previous line should say these
sed'1,1!d' somefile.txt | echo "$1",sed'1,1!d' somefile.txt | sed '1,1s/.*/'$1'/' FileToChange.txt"andsed'1,1!d' somefile.txt | cat $1where the contents of somefile.txt are these\n are\n words\n (each word being its own line of course) As stated, Cat is the only one that prints non blank lines.{}editing icon or pressctrl+k... you do not need to use\n... for the givensomefile.txtandtargetfile.txt, could you please add what is expected output? I'm still not clear what is your requirement (and not sure if you know what $1 means)