Add the word "prefix" to beginning of line using sed


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Add the word "prefix" to beginning of line using sed

SUSE linux
bash shell

this works
Code:
test -d /tmpp && echo "directory exists" || echo "directory doesn't exists"  |sed -e "s/^/prefix /"
   prefix directory doesn't exists

but why doesn't this work?
Code:
 test -d /tmp && echo "directory exists" || echo "directory doesn't exists"  |sed -e "s/^/prefix /"
   directory exists

# 3  
If you want it to work, both echo statements have to be part of one single process, I chose a subprocess:
Code:
( test -d /tmp && echo "directory exists" || echo "directory doesn't exists" ) | sed -e "s/^/prefix /"
 prefix directory exists

Otherwise the part after the || is part of the second compound statement (echo )not the first.

Last edited by jim mcnamara; 4 Weeks Ago at 06:58 PM..
These 5 Users Gave Thanks to jim mcnamara For This Post:
# 4  
I guess even a { code block; } is forced into a subshell because of the pipe.
An if-then-fi is a code block, too. The following is unusual but standard
Code:
if test -d /tmp; then
  echo "directory exists"
else
  echo "directory doesn't exist"
fi | sed -e "s/^/prefix /"

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
In Vi "sed" substitute word on a specific line
pimmit22043
i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ...... Shell Programming and Scripting
5
Shell Programming and Scripting
how to use "cut" or "awk" or "sed" to remove a string
timmywong
logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz... Shell Programming and Scripting
8
Shell Programming and Scripting
awk command to replace ";" with "|" and ""|" at diferent places in line of file
shis100
Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone...... Shell Programming and Scripting
7
Shell Programming and Scripting
Can "sed" substitute word on a specific line?
minifish
Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish... Shell Programming and Scripting
2
Shell Programming and Scripting
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"
Lokesha
Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers