Confused by the sed command


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Confused by the sed command

Hi Gurus,

I am a little bit confused by the sed command.

my file is below
Code:
 
cat sample
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii

below is command and output.
Code:
 
sed  '/ddd/{x;p;}' sample.txt 
aaa
bbb
ccc
 
       --blank
       --blank
eee
fff
ggg
hhh
iii

as per my understanding, {x;p;} when match found, x -- exchange hold place with pattern place which is null with ddd, then p; print, I thought it only print once, but here it prints twice.

Please help me to understand this, thanks in advance.

Ps, I think I figured out this. it works as below. x -- exchange hold place with pattern place which is empty. with ddd then p; print. after finish these two. the default is "print pattern space", so it prints the pattern space content again which is empty.

Please correct me if anything incorrect.

Last edited by ken6503; 03-30-2015 at 05:59 PM..
# 2  
sed -n may be your friend. Thus don't print unless you explicitly say so.
# 3  
Actually, in your sample output there are three blank lines that in real life aren't there; it's just two.
And yes, you are right with your analysis - for ddd lines, there is one explicit print after the xchange, and one implicit. If you want just the explicit one, append (man sed)
Quote:
d Delete pattern space. Start next cycle.
# 4  
This SED command and the MAN pages drive me nuts, its never as simple to follow and even if you use google its almost impossible to get a simple example and visual representation of what you actually need to do. Anybody can read a story , but just show the actual practicality of it, what?

I have a file which has broken lines, unintended line feeds.
I want to repair them, so my thoughts are , find "something" unique about those particular broken lines and then do something to make them append to the rest of the line accordingly.
I have found out how to do this in AWK and GAWK.

I wish to know how to do this using SED which i understand is the most correct method.
I understand i need to use SED '/find something/ {N something something, i dont undersgand the syntax and the material i have referenced doesn't explain this in a one liner so its just ridiculous.
I have tried for a month already on and off but i am not able to win here.

The character which is unique to the broken records is a "$ at the end of the line....which is CORRECT and just a $ character is the incorrect broken line.
So i need to say find lines that end in ONLY $ and then append the NEXT line to that line with the $ character only.
So again, the lines ending with "$ are OK and the lines ending with $ NOT OK.

I use cat -vet filename.txt to see the $ characters.

I know i need to use N as an option to SED.

Appreciate the simple example and answer. Thanks

Last edited by Imre; 04-01-2015 at 05:38 PM..
# 5  
@lmre: the n command seems more appropriate here.
To delete an empty line following a line ending with a "
Code:
sed '/"$/{n;/^$/d;}' file

This User Gave Thanks to MadeInGermany For This Post:
# 6  
still confussed about the sed command

Quote:
Originally Posted by MadeInGermany
@lmre: the n command seems more appropriate here.
To delete an empty line following a line ending with a "
Code:
sed '/"$/{n;/^$/d;}' file

I need to search for lines ending with $ and not "$
The i want to append the next line to that line,
I dont want to delete anything, i dont know why i need to delete in your example.
There is no empty line here.

23445w34,"djhahfhake,shdjgekagdgejs"$
23445w34,"djhahfhake","shdjgekagdgejs"$
23445w34,"kjshdfd","shdjg$
"."ekagdgejs"$
23445w34,"djhamdhzkjfsdtheree,jgekagdgejs"$
23445w34,"doubletroubkeinhungary","shdjgekagdgejs"$

Line 3 is the broken line and ends with $ and i want to APPEND line 4 to line 3.
Thats it.

I have used GAWK and AWK like this :
rskjhbvsfs1:icsoka:~/code> awk '{if ( /.$/ != /\"$/ ) printf $0 ; else print $0 } southafrica.txt


Now i want to win using SED.

Last edited by Imre; 04-01-2015 at 06:13 PM..
# 7  
Ah, then it's
Code:
sed '/"$/!{N;s/\n//;}' file

If not ending with a " append the next line, delete the embedded \n

---------- Post updated at 05:20 PM ---------- Previous update was at 03:18 PM ----------

The awk command is still easier here, an improved version is
Code:
awk '{if ( /"$/ ) print $0 ; else printf "%s",$0 }' file

sed lacks the capability to print a line without a newline at the end.
So more effort is needed.
Correct implementations with sed are, using a loop and a look-ahead append:
Code:
sed '                                                       
:loop
/"$/b
$!N;s/\n//
t loop
' file

or, using the hold space:
Code:
sed '
H;/"$/!{$!d;}
s/.*//;x;s/\n//g
' file


Last edited by MadeInGermany; 04-01-2015 at 08:37 PM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #734
Difficulty: Medium
Commodore BASIC was also known as PET BASIC.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused by command substitution and quoting

How do I assign output of a command to a variable without expanding it? I have tried every combination of quoting I can think of including literal quotes in the command and user agent variable and still can't get it to work (or it breaks Lynx because Lynx tolorates spaces in the useragent but... (2 Replies)
Discussion started by: Michael Stora
2 Replies

2. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. UNIX for Dummies Questions & Answers

Confused with sed...

Yes I know this is a bad use of cat, but just ignore it please :P My problem is with sed. File: "newFile" contains: day Command I run: cat newFile | sed 's/day/night' This returns: sed: -e expression #1, char 11: unterminated `s' command Why??? (1 Reply)
Discussion started by: maximus73
1 Replies

5. UNIX for Dummies Questions & Answers

Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies. I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it... (4 Replies)
Discussion started by: jazzmusic
4 Replies

6. Shell Programming and Scripting

SED - confused by the s command.

echo "abc 123" | sed 's/*/& &/g' output: a b c 123 123 Why there are spaces between the "abc" letters? echo "abc 123" | sed 's/*/&&/' output: abc 123 Why the regex in the above script does not match anything? I thought * should match 123 in any case. ---------- Post updated at 08:25... (3 Replies)
Discussion started by: kevintse
3 Replies

7. 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

8. Solaris

Confused with echo $SHELL Command....

Hi.. Everyone... Kindly consider following : login as: root Using keyboard-interactive authentication. Password: Last login: Mon Nov 3 19:30:50 2008 from xxxxxxxxxxx Sun Microsystems Inc. SunOS 5.10 Generic January 2005 You have new mail. Sourcing //.profile-EIS..... # # ... (3 Replies)
Discussion started by: Reboot
3 Replies

9. Shell Programming and Scripting

sed very confused

Hello experts, I have this complicated code that output value in between pattern and add "a string" to the front of the output. The problems I have many pattern that are the same. so I want to know how to get the value between 1st pattern, 2nd pattern etc. Any suggestions? sed -n... (14 Replies)
Discussion started by: minifish
14 Replies

Featured Tech Videos