Add new line after ' char


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new line after ' char
# 8  
Old 06-21-2012
Quote:
Originally Posted by Scrutinizer
Modifying ctsgnb's suggestion:
Code:
awk '{sub(/ *$/,x); $1=$1}1' FS="' " OFS="'\n" infile

Same issue again Smilie. For unformatted file its working perfectly fine.
but if file is already formatted, its adding new lines in between.

Can we do following:
Remove all new line character first even if file is formatted (data will be in one straight line and then execute above code ?

Please advice

Regards,
# 9  
Old 06-21-2012
Can you post a sample of a file where it goes wrong, I cannot reproduce this. What OS and version are you using?. Can you run one of the lines ending in ,' through od -c ?

Last edited by Scrutinizer; 06-21-2012 at 06:16 AM..
# 10  
Old 06-21-2012
Put the following script in a file named "awkscr".

Code:
!/' *$/ {gsub("' ","' \n")}
1

and then run

Code:
awk -f awkscr inputfile


sed is better suited for such an editing requirement. Create a sed script file file say "sedscr" and enter following lines in it:

Code:
s|\(' *\)\([A-Za-z0-9]\)|\1\
\2|g

and then use

Code:
sed -f sedscr inputfile


Last edited by elixir_sinari; 06-21-2012 at 06:37 AM..
# 11  
Old 06-21-2012
Quote:
Originally Posted by Scrutinizer
Can you post a sample of a file where it goes wrong, I cannot reproduce this. What OS and version are you using?. Can you run one of the lines ending in ,' through od -c ?
I am using Linux OS, following is the output of od -c command.
Code:
 
0000000   S   T   G   ,   1   2   3   4   5   ,   0   0   0   ,   9   9
0000020   9   ,   '   P   Q   R   ,       2   3   4   5   ,   0   0   0
0000040   ,   9   9   9   ,   '   X   Y   Z   ,   7   8   9   0   ,   0
0000060   0   0   ,   9   9   9   ,  \n  \n
0000071

# 12  
Old 06-21-2012
Hi, I meant of the input file. And could you post a sample of the input file that goes wrong?
# 13  
Old 06-21-2012
Quote:
Originally Posted by elixir_sinari
Put the following script in a file named "awkscr".

Code:
!/' *$/ {gsub("' ","' \n")}
1

and then run

Code:
awk -f awkscr inputfile


sed is better suited for such an editing requirement. Create a sed script file file say "sedscr" and enter following lines in it:

Code:
s|\(' *\)\([A-Za-z0-9]\)|\1\
\2|g

and then use

Code:
sed -f sedscr inputfile

Thanks alot. SED is working fine for both formatted and unformatted files.

One question, [A-Za-z0-9] means all capital and small chars also numbers.
I am working on UK file which might have some special chars like é, £, €

Can we include such chars as well ?

Please advice.

Regards

---------- Post updated at 07:35 AM ---------- Previous update was at 07:30 AM ----------

Quote:
Originally Posted by Scrutinizer
Hi, I meant of the input file. And could you post a sample of the input file that goes wrong?
Following are the lines from formatted file. See additional blank lines after each line.
Code:
STG,12345,000,999,'
 
PQR, 2345,000,999,'
 
XYZ,7890,000,999,

# 14  
Old 06-21-2012
Yes, but that is the output file isn't it?
If that is the input file then what should the output file look like?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to add a numeric & special char to end of the first line

Need to add a numeric & special char to end of the first line Existing file: 12-11-16|11 2016 Jan 12:34:55|03:55| 13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55| 14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55| 15-10-16|18 2016 Jan... (11 Replies)
Discussion started by: Joselouis
11 Replies

2. Shell Programming and Scripting

How to separate one line to mutiple line based on one char?

Hi Gurus, I need separate one file which is one huge line to mutiple line. file like abcd # bcd # def # fge # ged I want to get abcd bcd def fge ged Thanks in advance (4 Replies)
Discussion started by: ken6503
4 Replies

3. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

4. Shell Programming and Scripting

Add char before certain line

I'm trying to add a '1' before a line that has the word "C_ID" s/.*C_ID.*/&\n1/ The above code did what I need, but the '1' is added after the C_ID word :( Help please. (5 Replies)
Discussion started by: newbeee
5 Replies

5. UNIX Desktop Questions & Answers

add char o end of line if dosent exist

hey , i want to check if the char "#" exist at the end of every line of txt file and if it dosent then add it for example: the cat jumped on my mom # cars can run on water# i cant get a date blue yellow# will be: the cat went back home# cars can run on water# i cant get a... (2 Replies)
Discussion started by: boaz733
2 Replies

6. UNIX for Dummies Questions & Answers

Check if last char in a line is '.' and if not, insert it

Hi all, I've searched for this but couldn't seem to find the answer I'm looking for. I have a text file that looks like this: sgea0447 Earnings followed the same path. earnings followed the same path sgea0448 Economic growth slowed further. economic growth slowed further... (13 Replies)
Discussion started by: pxalpine
13 Replies

7. UNIX for Dummies Questions & Answers

wc -c counts 1 char more per line

Hi, this might be a basic question... why is that wc -c counts 1 more per line than what is there. for example, > cat dum1.txt 123 12 > wc -c dum1.txt 7 dum1.txt Thanks, Sameer. (4 Replies)
Discussion started by: ensameer
4 Replies

8. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

9. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

10. Shell Programming and Scripting

extraction of last but one char in a line

I need to extract the character before the last "|" in the following lines, which are 'N' and 'U'. The last "|" shouldn't be extracted. Also the no.s of "|" may vary in a line, but I need only the character before the last one. ... (1 Reply)
Discussion started by: hidnana
1 Replies
Login or Register to Ask a Question