how to edit a content of a document to add \n after each ;


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to edit a content of a document to add \n after each ;
# 1  
Old 04-26-2011
how to edit a content of a document to add \n after each ;

Hi:

I'm trying to write a script that reformats a given document (edit the content of a given document) , I want to add a new line \n after each ; in the document.

Regards
# 2  
Old 04-26-2011
Look into: dos2unix (maybe called dos2ux on your box)
Code:
man dos2unix

turns windows text file into unix text file.
# 3  
Old 04-26-2011
assuming a carriage return and not the string "\n"...

This also assumes you don't want a newline at the end of every line, else the above post is your answer. You didn't say if every line ended in a ';'.

Code:
sed 's/;/;<Ctrl-v><enter>/g' filename > filename.out

One way is to use the stream editor sed. Search for a ';' and replace it with a ';' followed by a <newline> on every occurrence on a line. Note pressing Ctrl-V allows one to enter a control character.

Good Luck,
Gary

Last edited by gary_w; 04-26-2011 at 06:18 PM..
This User Gave Thanks to gary_w For This Post:
# 4  
Old 04-26-2011
I've tried the script below

Code:
sed 's/;/;\\n/' testing.txt > testing2.txt

but that didn't work, the new line wasn't added after each ; in the output file (testing2.txt)

---------- Post updated at 05:46 PM ---------- Previous update was at 05:26 PM ----------

This worked perfectly Smilie
Thank you.

Quote:
Originally Posted by gary_w
This also assumes you don't want a newline at the end of every line, else the above post is your answer. You didn't say if every line ended in a ';'.

Code:
sed 's/;/;<Ctrl-v><enter>/g' filename > filename.out

One way is to use the stream editor sed. Search for a ';' and replace it with a ';' followed by a <newline> on every occurrence on a line. Note pressing Ctrl-V allows one to enter a control character.

Good Luck,
Gary
# 5  
Old 04-26-2011
Glad to help

As you have found out, a '\n' represents a newline when used in the echo or print commands, depending on what shell you are using.

Regular expressions for pattern matching are another story.

Blue Skies,

Gary
# 6  
Old 04-26-2011
Quote:
I've tried the script below


Code:
sed 's/;/;\\n/' testing.txt > testing2.txt
but that didn't work, the new line wasn't added after each ; in the output file (testing2.txt)
The sed command above would replace only the first occurrence of ; on a line -- to replace ALL semicolons, you need to specify global replacement:
Code:
sed 's/;/;\\n/g'

# 7  
Old 04-26-2011
Quote:
Originally Posted by mirni
The sed command above would replace only the first occurrence of ; on a line -- to replace ALL semicolons, you need to specify global replacement:
Code:
sed 's/;/;\\n/g'

That will replace every instance of a semicolon with a semicolon, backslash, and "n".

The portable way of including a newline in the replacement text requires preceding an actual newline character with a backslash (posix does not recognize \n in the replacement text).

Code:
sed 's/;/;\
/g'

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated. My example: sed -i '14 i\ # add these lines add these lines to... (5 Replies)
Discussion started by: Nvizn
5 Replies

2. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

3. Shell Programming and Scripting

Help with add new line in between each content

Input file: 48458951 49529947 46700865 46207063 52639785 47012578 55872838 49258996 Desired output file: 48458951 49529947 46700865 46207063 (1 Reply)
Discussion started by: perl_beginner
1 Replies

4. Shell Programming and Scripting

Edit file content at the specific line.

How to edit file content at the specific line? For example at below The things to edit --> This is line 2. And it is below line 1. This is line 1. This is line 2. # i want to append some words at this row line. How? This is line 3. (8 Replies)
Discussion started by: alvin0618
8 Replies

5. Shell Programming and Scripting

edit and add line

dear all, i need your help to change this input to output M9_3D_H10__Dflt ->SP_M9N_S 497224.3125 1598028.1250 497063.2813 1598002.7500 496953.1250 1597951.8750 497122.6250 1597985.7500 497190.4375 1597994.2500... (3 Replies)
Discussion started by: ipatah
3 Replies

6. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

7. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

8. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

9. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question