Clarification needed for a SED one liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Clarification needed for a SED one liner
# 1  
Old 12-08-2010
Clarification needed for a SED one liner

I want to use SED to replace all new line characters of a file, I googled and found this one liner
Code:
sed '{:q;N;s/\n//g;t q}' infile

what do :q;N; and t q mean in this script?
# 2  
Old 12-08-2010
:q is a label (called q), N reads the next line (the two lines now also contain the newline character, which the substitution (s) will remove), t q go's back (t) to the label q in the event that the substitution (s) had modified anything.
This User Gave Thanks to Scott For This Post:
# 3  
Old 12-08-2010
Oh, after I tested the script, it did not delete new line characters, the file is unchanged.
How to delete new line characters using SED?
# 4  
Old 12-08-2010
What exactly u want to do? Are u looking to merge all lines or u have ^M characters in your file which needs to be deleted?
R0H0N
# 5  
Old 12-08-2010
Hi, ROHON
I want to delete the last new line character(\n) in the file. what's ^M by the way?
# 6  
Old 12-08-2010
Quote:
Originally Posted by kevintse
Oh, after I tested the script, it did not delete new line characters, the file is unchanged.
How to delete new line characters using SED?
The file might be unchanged because you didn't save it?

If your sed has the -i option, use it to overwrite the file with the changes, otherwise write to a temporary file and then replace the original one. Otherwise use perl or ed to make the changes and save the file.
# 7  
Old 12-08-2010
Quote:
Originally Posted by kevintse
I want to use SED to replace all new line characters of a file
To replace all new line characters of a file you can use xargs:
Code:
xargs < file > newfile

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed one Liner option -e

Hi, I have the following command.(Delete all trailing blank lines at the end of a file.) sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' I don't understand the logic of this command and also I don't understand why -e is used. Can you please let me know the logic of this command and why three -e... (5 Replies)
Discussion started by: TomG
5 Replies

2. UNIX for Dummies Questions & Answers

awk or sed one liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: XXXX AAA234 XXXX BBB678 XXXX CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? XXXX AAA234... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. UNIX for Dummies Questions & Answers

sed one-liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: AAA234 BBB678 CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? AAA234 Thing1 BBB678 Thing2... (5 Replies)
Discussion started by: jimmyf
5 Replies

4. UNIX for Dummies Questions & Answers

Clarification required on sed

Hi Can some one tell what does this sed command do sed 's/*$//g I am more curious on the highlighted part , can some one explain what does that mean. Thanks Sri (1 Reply)
Discussion started by: Sri3001
1 Replies

5. Shell Programming and Scripting

help with sed one liner

hey everyone, I want to remove some characters from a string that i have with sed. For example if my string is: a0=bus a1=car a2=truck I want my output to look like this: bus car truck So i want to delete the two characters before the = and including the =. This is what i came up with... (3 Replies)
Discussion started by: GmGeubt
3 Replies

6. UNIX for Advanced & Expert Users

one liner needed

Hi I have a file say (a.txt) which has following a.txt ---- $$var1=Tom $$var2=Kim I need a one liner which searches the file(a.txt) for $$var1 and returns the value in it(Tom). Thanks in advance Ammu (7 Replies)
Discussion started by: ammu
7 Replies

7. Shell Programming and Scripting

Help Needed with 1 liner AWK statement

Greetings, I attempting to create a line statement that will do the following: 1. Read the input file 2. Extract lines containing certain keep words 3. Print the lines in a tabular format (CSV) Help is what I have so far: # cat text.tx | egrep -i -e "(Check Name) | (Risk Level) |... (6 Replies)
Discussion started by: jroberson
6 Replies

8. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies
Login or Register to Ask a Question