insert new line at found chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert new line at found chars
# 1  
Old 09-25-2008
insert new line at found chars

Hey gang,

I have:
XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd

That is all on one line. Very simply put I want to do is something like:
sed s'/XXZZXXZZXX /\n/g'
or
tr 'XXZZXXZZXX ' '/n'

I have tried various things but can never get the desired results.

I want to end up with:
123 asdaffggh dfghyrgr ertyhdhh
234 sdg
456 gfg fggfd
# 2  
Old 09-25-2008
The sed solution is basically correct. I'm guessing your sed doesn't understand \n to mean newline, so you need to use a literal newline or something.

Code:
sed 's/XXZZXXZZXX /
/g'

Yes, that's a literal newline after the second slash. You might need to put a backslash in front.

tr is out of the question; it only replaces individual characters.
# 3  
Old 09-25-2008
I tried that earlier I get:
Code:
# /usr/bin/sed 's/XXZZXXZZXX /
> /g' /myfile
sed: command garbled: s/XXZZXXZZXX /
#

I am unclear what you meant by the backslash in front, but I just tried some things and got this to work:
Code:
# /usr/bin/sed 's/XXZZXXZZXX /\
> /g' /myfile

It now works, Thanks!
# 4  
Old 09-25-2008
Change that, I am still having issues. I added a newline in vi when I was testing. The source file has no newline and it seems sed will not even read it. My output from previous post is empty with correct source file. I try and add a newlne so sed will read it with
Code:
# /usr/bin/sed '$a\
> ' /myfile

My output is empty again. I guess I need a command to add a new line before I can have sed work with it? Thoughts?
# 5  
Old 09-25-2008
You mean that the input data file has no newline at the end, and your sed will not cope with such a file?

There are some tools which will add a missing newline on the last line as a side effect, but with any luck, none of them are installed on your system. Try awk for a start. You might as well do the substitution in awk too then.

Code:
awk '{ gsub ("XXZZXXZZXX ", "\n") }1' file


Last edited by era; 09-25-2008 at 04:21 PM.. Reason: Missing 1 after close brace
# 6  
Old 09-25-2008
Quote:
Originally Posted by era
You mean that the input data file has no newline at the end, and your sed will not cope with such a file?
Yes

Awk did:
Code:
# /usr/bin/awk '{ gsub ("XXZZXXZZXX ", "\n") }1' /myfile
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
#

This added a new line
Code:
echo >>myfile

Then I used the sed from above. I am more familiar with sed. Thanks again!
# 7  
Old 09-25-2008
For what it's worth, the gsub function is an addition which was not present in the original Aho Weinberger Kernighan version of awk. Perhaps you could find nawk, mawk, gawk, or XPG4 awk on your system.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a certain char and insert new text if a match found

Have a file which has the create statement like below create table emp ( empno integer, empname char(50)) primary index(empno); i need to find a string starting with create and ends with semi-colon ;. if so insert the below statement before create statement rename table emp to emp_rename;... (2 Replies)
Discussion started by: Mohan0509
2 Replies

2. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

3. UNIX for Dummies Questions & Answers

Grep with special chars line by line

I have a file that includes strings with special characters, eg file1 line: 1 - special 1 line: = 4 line; -3 etc How can I grep the lines of file1 from file2, line by line? I used fgrep and egrep to grep a particular line and worked fine, but when I used: cat file1|while read line;do... (2 Replies)
Discussion started by: FelipeAd
2 Replies

4. UNIX for Dummies Questions & Answers

Grep or sed to search, replace/insert chars!

HI All Im trying to come up with an approach to finding a string, using a portion of that string to insert it on lines starting with the value "GOTO" appending to end of line after removing PT's ( See example below! ) EXAMPLE: 1. I would like to search for the line that starts with "TLAXIS/"... (7 Replies)
Discussion started by: graymj
7 Replies

5. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

6. Shell Programming and Scripting

Insert specific line when found similar value

Hi All, I have file like this: file1: 3778 10474 24 3778 10475 24 3778 10476 25 3778 10495 26 3794 10001 33 3794 10002 33 3794 10004 33 3794 10007 34 3794 10008 34 3794 10011 34 3794 10012 34 3794 10013 34 3794 10017 34 3810 10282 27 (6 Replies)
Discussion started by: attila
6 Replies

7. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

8. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

9. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question