Insert New Line Character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert New Line Character
# 1  
Old 12-25-2013
Insert New Line Character

Hi,
If my first character of a line starts with 2 then after 5th charecter newline character should be inserted.

Input.txt:
Code:
a1234567890
2222300007
bsdfsdf888999999
ssdfkjskfdjskfdjd
2899900000000099999999999999
28887777
999999999999999999

Output.txt:
Code:
a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999

I've tried the below but not getting desired output, could you please help me in this.
Thanks in advance

Code:
awk 'BEGIN{ FS=""; ch=5}
{
 z=substr($0,0,1) ;
if  (z== 2)
    print substr($0,1,ch) "\n" substr($0,ch)
}' Input.txt>Output.txt

Regards,
Ulf

---------- Post updated at 11:57 AM ---------- Previous update was at 11:23 AM ----------

I got the output...... Its working perfectly
Code:
awk 'BEGIN{ FS=""; ch=5}
{
    if (substr($0,0,1) == 2)
    print substr($0,1,ch) "\n" substr($0,ch)
    else
    print $0
}' Input.txt>Output.txt

# 2  
Old 12-25-2013
Are you sure that is doing what you want?

It looks like there might be an off by 1 error. I would have thought you would want to change the line:
Code:
    print substr($0,1,ch) "\n" substr($0,ch)

to:
Code:
    print substr($0,1,ch) "\n" substr($0,ch+1)

to get the output you said you wanted.
# 3  
Old 12-25-2013
Try also (untested):
Code:
awk '{sub /^2..../, "&\n")}1' file

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 12-26-2013
Hello,

One more approach with awk.

Code:
awk '/^2/ {print (substr($0,1,5)"\n"substr($0,6))} !/^2/ { print $0 }' file_name

Output will be as follows.

Code:
a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-27-2013 at 04:43 AM.. Reason: Edited as per requirement for getting 5 char in string
# 5  
Old 12-26-2013
Quote:
Originally Posted by RavinderSingh13
Hello,

One more approach with awk.

Code:
awk '/^2/ {print (substr($0,1,4)"\n"substr($0,5)) }1' file_name

Output will be as follows.

Code:
a1234567890
2222
300007
2222300007
bsdfsdf888999999
ssdfkjskfdjskfdjd
2899
900000000099999999999999
2899900000000099999999999999
2888
7777
28887777
999999999999999999


Thanks,
R. Singh
That is interesting, but the lines marked in red above do no match what was requested. The request was to replace lines starting with a "2" with two lines where the 1st replacement line contains the 1st five characters of that line and the 2nd replacement line contains the rest of that line.

Your code adds two lines instead of replacing one line with two, and the 1st added line only contains the 1st four characters from that line instead of the 1st five characters.
# 6  
Old 12-27-2013
RavinderSingh13 :

Code:
$ awk '/^2/{print (substr($0,1,4)"\n"substr($0,5)) }1' file

Duplicate lines were getting printed as noticed by Don Cragun because of both printand 1, whenever pattern is found you should not use 1 at the end along withprint in beginning, you need to use some variable something like this, to avoid line getting printed.
Code:
$ awk '{ f = /^2/ ?  0 : 1 }!f{print substr($0,1,5) RS substr($0,6)}f' file

OR simply like this
Code:
$ awk '/^2/{$0 = substr($0,1,5) RS substr($0,6)}1' file

Resulting
Code:
a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999

# 7  
Old 12-27-2013
Quote:
POSTED By Don Cragun:
That is interesting, but the lines marked in red above do no match what was requested. The request was to replace lines starting with a "2" with two lines where the 1st replacement line contains the 1st five characters of that line and the 2nd replacement line contains the rest of that line.

Your code adds two lines instead of replacing one line with two, and the 1st added line only contains the 1st four characters from that line instead of the 1st five characters.
below code should be good I have put 1 so it printed the lines 2 times.

Code:
awk '/^2/ {print (substr($0,1,5)"\n"substr($0,6))} !/^2/ { print $0 }' file_name


Output will be as follows.


Code:
a1234567890
2222
300007
bsdfsdf888999999
ssdfkjskfdjskfdjd
2899
900000000099999999999999
2888
7777
999999999999999999


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-27-2013 at 04:44 AM.. Reason: changed the code as per requirement.
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

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

3. UNIX for Dummies Questions & Answers

Insert sign every n character (awk)

Hi, For example, I would like to insert a pipe every 4 characters for each second field (including after the last block). input (coma separated): line1,AAAABBBBCCCCDDDDEEEE line2,FFFFGGGGHHHHIIIIJJJJ output: line1,AAAA|BBBB|CCCC|DDDD|EEEE| line2,FFFF|GGGG|HHHH|IIII|JJJJ| my... (2 Replies)
Discussion started by: beca123456
2 Replies

4. Shell Programming and Scripting

Insert a character before a line

I have a file and I can get the line with a specific pattern. I want to inset # on start of the line. file.text ==== aa bb cc bb hh kk kk ll yy dd aa kk rr tt aa I want to comment out the line with contain "aa" after running the script file.text ==== #aa bb cc bb hh kk kk ll... (7 Replies)
Discussion started by: Biplab
7 Replies

5. Shell Programming and Scripting

sed to insert a character

Hi all, I have a file like this Q8N302 21-84 Q8N157 15-45 Q99996 167-201 202-251 269-318 I want to insert a character or space if the line starts with a number and I used the command sed 's/^/#/' But in the output file, when it inserts this character, first digit in the number is... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. UNIX for Dummies Questions & Answers

Insert character in the line

Hi All, I have below type file. abc|asd|pqr|2|2|2 asc|qwe|scf|5|4|4 Pipe location and count is dynamic and coming from a variable. I want to change it to below files. (chnage the first pipe to 3 pipes) abc|||asd|pqr|2|2|2 asc|||qwe|scf|5|4|4 (chnage the second pipe to 4 pipes)... (1 Reply)
Discussion started by: swat
1 Replies

9. Shell Programming and Scripting

How to insert a character in line in Special condition?

Hi, I have a log file generated by a tool which has the following look : /tmp/releases/directory/datefilename1_release_date.zip /tmp/releases/directory/datefilename2_release_date.zip /tmp/releases/directory/datefilename3_release_date.zip... (8 Replies)
Discussion started by: bhaskar_m
8 Replies

10. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question