Add new line before specific character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new line before specific character
# 1  
Old 01-06-2012
Add new line before specific character

hello all,

I have file like this "infile.txt" (all data in one line)

Code:
A240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232 582249.29974388.5 8.01A 231 582249.49974401.0 8.0 230582249.59974413.4 8.0 229 582249.79974425.9 8.04B02-118      1    7491 01412.07S1174425.53E 582383.09973839.5 53.9244231912 C-118      11   7491 014 3.72S1174425.00E 582366.59974095.7 53.9244231912D-118      11   7491 014 3.72S1174425.00E 582366.59974095.7 56.0244231912 D-118      12   7491 014 3.94S1174426.59E 582415.69974088.9 56.0244231956

I want to make output: (several lines)

Code:
A240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01
A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01
A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232 582249.29974388.5 8.01
A 231 582249.49974401.0 8.0 230582249.59974413.4 8.0 229 582249.79974425.9 8.04
B02-118      1    7491 01412.07S1174425.53E 582383.09973839.5 53.9244231912 
C-118      11   7491 014 3.72S1174425.00E 582366.59974095.7 53.9244231912
D-118      11   7491 014 3.72S1174425.00E 582366.59974095.7 56.0244231912 
D-118      12   7491 014 3.94S1174426.59E 582415.69974088.9  56.0244231956

Thanks,

Attila

Last edited by joeyg; 01-06-2012 at 12:56 PM.. Reason: Please wrap data in CodeTags; not html
# 2  
Old 01-06-2012
Try
Code:
sed 's/\([ABCD] \)/\n\1/g;s/\([ABCD][0-9]\)/\n\1/g' < infile > outfile

If the letters are ever anything but ABCD like you have here, the expression will need to be changed.

---------- Post updated at 09:56 AM ---------- Previous update was at 09:44 AM ----------

Actually I can simplify that into sed 's/\([ABCD][0-9 ]\)/\n\1/g' < infile > outfile
# 3  
Old 01-06-2012
Corona, How about "B02"?

For example, when I have other case like this:

A240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232 582249.29974388.5 8.01A 231 582249.49974401.0 8.0 230582249.59974413.4 8.0 229 582249.79974425.9 8.04B0205J-118 1 7491 01412.07B1174425.53E 582383.09973839.5 53.9244231912 C0205J-118 11 7491 014 3.72S1174425.00E 582366.59974095.7 53.9244231912 D0205J-118 11 7491 014 3.72S1174425.00E 582366.59974095.7 56.0244231912 D0205J-118 12 7491 014 3.94S1174426.59E 582415.69974088.9 56.0244231956

result:

A
240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01
A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01
A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232 582249.29974388.5 8.01
A
231 582249.49974401.0 8.0 230582249.59974413.4 8.0 229 582249.79974425.9 8.04
B0205J-118 1 7491 01412.07B1174425.53E 582383.09973839.5 53.9244231912
C
0205J-118 11 7491 014 3.72S1174425.00E 582366.59974095.7 53.9244231912
D
0205J-118 11 7491 014 3.72S1174425.00E 582366.59974095.7 56.0244231912
D0205J-118 12 7491 014 3.94S1174426.59E 582415.69974088.9
56.0244231956

there are two similar text, "B", in one line.

Thanks,
# 4  
Old 01-06-2012
What about it? My code, both versions, handled that already.

It adds a newline whenever it sees the letter A-D immediately followed by a space. It also adds a newline whenever it sees A-D immediately followed by a digit.

[edit] Oh, I see, you want to exclude that B117. Hmm.

---------- Post updated at 10:32 AM ---------- Previous update was at 10:26 AM ----------

Code:
sed 's/\([A-D]\) /\n\1/g;s/B02/\nB02/g'

Whenever it sees A-D followed by a space, it changes that to newline A-D.

Whenever it sees B02, it changes it to \nNB02.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-06-2012
can I input spesific character as "B02" not "B"? when I tried to input "B", I was getting different result.

---------- Post updated at 12:03 PM ---------- Previous update was at 11:41 AM ----------

Great Corona, it working. :-)

---------- Post updated at 12:03 PM ---------- Previous update was at 12:03 PM ----------

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert new line after a specific character in scripts?

Hi, I'm trying to add a new line after finding a specific String. That is my string: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html and I want to change that file to: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html extra_hosts: -... (4 Replies)
Discussion started by: siamak
4 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

4. Shell Programming and Scripting

Removing last character of a specific line from a file

Hello guys, I would need to remove the last character ")" of a specific line. This can be from any line. Your help is appreciated. Below is the line. HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)) Please help. (6 Replies)
Discussion started by: sang8g
6 Replies

5. Shell Programming and Scripting

Remove line with specific character

HI Input :- Aog:0rt__dev_8 LAAXU24 vs.3 LAA40l0 ** LAAXU241 ** Output :- Aog:0rt__dev_8 LAAXU24 vs.3 Delete the line with ** (3 Replies)
Discussion started by: pareshkp
3 Replies

6. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

7. Shell Programming and Scripting

new line after specific number character

Hi All, I have input file like this: input1: ( 1083479)=T 158V 1798, T 391V 1896,T 1138V 2273,T 1547V 2477,T 2249V 2917,T 3278V 3234,T 4152V 3495,T 5500V 3631, ( 1083501)=T 181V 1851, T 459V 1954,T 810V 2141,T 1188V 2372,T 1638V 2696,T 2731V 3124,T 4799V 3640,... (5 Replies)
Discussion started by: attila
5 Replies

8. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

9. Shell Programming and Scripting

Extract character between specific line numbers

Hi guys, I have txt file and I would need to extract all the contents between specific line numbers. Line 1: apple Line 2: orange Line 3: mango Line 4: grapes Line 5: pine apple I need to extract the content between line 2 and 4, including the contents of Line 2 and 4 so the ouput... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

10. Shell Programming and Scripting

How to append a character to the last but one field on a specific line?

Hi Guys, I have a file like this: aaa b c d e f fsss g h i k l qqq r t h n I want: aaa b c d e f fsss g h i k l qqq r t h , n ggg p t e d u qqq i o s , k (2 Replies)
Discussion started by: npatwardhan
2 Replies
Login or Register to Ask a Question