SED remove line feed and add to certain area


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED remove line feed and add to certain area
# 1  
Old 09-25-2009
SED remove line feed and add to certain area

Hi All,

I have a xml file and requirement is to remove the line feed and add line feed after some element.

Code:
<?xml version="1.0" ?>
<AUDITRECORDS>
   <CARF>
      <HED>
         <VN1>20090616010622</VN1>
         <VN2>0</VN2>
         <VN3>1090</VN3>
         <VN4>CONFIG_DATA</VN4>
         <VN5>20090616010622</VN5>
         <VN6>0</VN6>
         <VN7>1090</VN7>
      </HED>
   </CARF>
   <CARF>
      <HED>
         <VN1>20090616010651</VN1>
         <VN2>0</VN2>
         <VN3>1130</VN3>
         <VN4>11LOWE</VN4>
         <VN5>20090616010651</VN5>
         <VN6>0</VN6>
         <VN7>1130</VN7>
      </HED>
   </CARF>
</AUDITRECORDS>

The output needed as below:

Code:
<?xml version="1.0" ?>
<AUDITRECORDS>
<CARF><HED><VN1>20090616010622</VN1><VN2>0</VN2><VN3>1090</VN3><VN4>CONFIG_DATA</VN4><VN5>20090616010622</VN5><VN6>0</VN6><VN7>1090</VN7></HED></CARF>
<CARF><HED><VN1>20090616010651</VN1><VN2>0</VN2><VN3>1130</VN3><VN4>11LOWE</VN4><VN5>20090616010651</VN5><VN6>0</VN6><VN7>1130</VN7></HED></CARF>
</AUDITRECORDS>

Please advice.

Regrads,
Sreejit

Last edited by Franklin52; 09-28-2009 at 08:23 AM.. Reason: Please use code tags!
# 2  
Old 09-25-2009
Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk).
Code:
awk -F'[<|>]' '{ORS=($2~"xml\|AUDITRECORDS\|\/CARF")?RS:OFS}1' OFS="" file

# 3  
Old 09-25-2009
Hi Danmero,

I am getting this error while running the command

Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Please advice.

Regards,
Sreejit

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

Hi Danmero,

Sorry I have main xml file as :
Code:
<?xml version="1.0" ?><AUDITRECORDS>
   <CARF>
      <HED>
         <VN1>20090616010622</VN1>
         <VN2>0</VN2>
         <VN3>1090</VN3>
         <VN4>CONFIG_DATA</VN4>
         <VN5>20090616010622</VN5>
         <VN6>0</VN6>
         <VN7>1090</VN7>
      </HED>
   </CARF>
   <CARF>
      <HED>
         <VN1>20090616010651</VN1>
         <VN2>0</VN2>
         <VN3>1130</VN3>
         <VN4>11LOWE</VN4>
         <VN5>20090616010651</VN5>
         <VN6>0</VN6>
         <VN7>1130</VN7>
      </HED>
   </CARF>
</AUDITRECORDS>

Please see if you can help

Regards,
Sreejit

Last edited by Franklin52; 09-28-2009 at 08:24 AM.. Reason: Please use code tags!
# 4  
Old 09-25-2009
  1. Use a different awk, works for me using
    Code:
    # awk --version
    awk version 20070501 (FreeBSD)

  2. Fix your second xml file or find the workaround by yourself, my solution works for original data sample.
    Code:
    # cat file
    <?xml version="1.0" ?>
    <AUDITRECORDS>
    <CARF>
    <HED>
    <VN1>20090616010622</VN1>
    <VN2>0</VN2>
    <VN3>1090</VN3>
    <VN4>CONFIG_DATA</VN4>
    <VN5>20090616010622</VN5>
    <VN6>0</VN6>
    <VN7>1090</VN7>
    </HED>
    </CARF>
    <CARF>
    <HED>
    <VN1>20090616010651</VN1>
    <VN2>0</VN2>
    <VN3>1130</VN3>
    <VN4>11LOWE</VN4>
    <VN5>20090616010651</VN5>
    <VN6>0</VN6>
    <VN7>1130</VN7>
    </HED>
    </CARF>
    </AUDITRECORDS>
    # awk -F'[<|>]' '{ORS=($2 ~ "xml\|AUDITRECORDS\|\/CARF")?RS:OFS}1' OFS="" file
    <?xml version="1.0" ?>
    <AUDITRECORDS>
    <CARF><HED><VN1>20090616010622</VN1><VN2>0</VN2><VN3>1090</VN3><VN4>CONFIG_DATA</VN4><VN5>20090616010622</VN5><VN6>0</VN6><VN7>1090</VN7></HED></CARF>
    <CARF><HED><VN1>20090616010651</VN1><VN2>0</VN2><VN3>1130</VN3><VN4>11LOWE</VN4><VN5>20090616010651</VN5><VN6>0</VN6><VN7>1130</VN7></HED></CARF>
    </AUDITRECORDS>



---------- Post updated at 03:21 PM ---------- Previous update was at 01:10 PM ----------

When I try to reply to your second post i seen the original file format Smilie .... PLEASE read the Forum Rules and Guidelines and use [code] tags when you post data sample or code.

Code:
# cat f1.xml
<?xml version="1.0" ?><AUDITRECORDS>
   <CARF>
      <HED>
         <VN1>20090616010622</VN1>
         <VN2>0</VN2>
         <VN3>1090</VN3>
         <VN4>CONFIG_DATA</VN4>
         <VN5>20090616010622</VN5>
         <VN6>0</VN6>
         <VN7>1090</VN7>
      </HED>
   </CARF>
   <CARF>
      <HED>
         <VN1>20090616010651</VN1>
         <VN2>0</VN2>
         <VN3>1130</VN3>
         <VN4>11LOWE</VN4>
         <VN5>20090616010651</VN5>
         <VN6>0</VN6>
         <VN7>1130</VN7>
      </HED>
   </CARF>
</AUDITRECORDS>

# awk -F'[<|>]' '{sub(/^[ \t]+/, "");gsub("><",">\n<");ORS=($2~"xml\|AUDITRECORDS\|\/CARF")?RS:OFS}1' OFS="" file.xml
<?xml version="1.0" ?>
<AUDITRECORDS>
<CARF><HED><VN1>20090616010622</VN1><VN2>0</VN2><VN3>1090</VN3><VN4>CONFIG_DATA</VN4><VN5>20090616010622</VN5><VN6>0</VN6><VN7>1090</VN7></HED></CARF>
<CARF><HED><VN1>20090616010651</VN1><VN2>0</VN2><VN3>1130</VN3><VN4>11LOWE</VN4><VN5>20090616010651</VN5><VN6>0</VN6><VN7>1130</VN7></HED></CARF>
</AUDITRECORDS>


Last edited by danmero; 09-25-2009 at 02:32 PM.. Reason: Fix spelling
# 5  
Old 09-28-2009
Hi Danmero,

Thanks for help and solution.
I think my awk version is different, I am getting the same error.

But thanks for ur solution.

Regards,
Sreejit

---------- Post updated at 12:19 PM ---------- Previous update was at 10:30 AM ----------

Hi Danmero,

I have used nawk and it is working, is it problem if we use nawk?

I am not strong in unix. I have understood some of the line used in ur command.

I may sound greedy
But if you can help, can you please let me know the what is -F '[<|>]' is this say that whatever in between <> take it as input.

sub(/^[ \t]+/, ""); means change all tab to blank

gsub("><",">\n<"); means to change the >< with new line in between.

Sorry I didn't understand ORS=($2~"xml\|AUDITRECORDS\|\/CARF")?RS:OFS}1

If you can please explain.

But anyway thanks a lot for your help.

Regards,
Sreejit
# 6  
Old 09-28-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 7  
Old 09-28-2009
Hi Danmero,

I have used nawk and it is working, is it problem if we use nawk?

I am not strong in unix. I have understood some of the line used in ur command.

I may sound greedy
But if you can help, can you please let me know the what is
Code:
-F '[<|>]'

is this say that whatever in between <> take it as input.

Code:
sub(/^[ \t]+/, "");

means change all tab to blank

Code:
gsub("><",">\n<");

means to change the
Code:
><

with new line in between.

Sorry I didn't understand
Code:
ORS=($2~"xml\|AUDITRECORDS\|\/CARF")?RS:OFS}1

If you can please explain.

But anyway thanks a lot for your help.

Regards,
Sreejit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line feed in data

Please use code tags for sample data Hi I have a file where there are line feeds in the data. I am not able to read the file from an application. I exported this data from Access database and many columns contain line feed. My data looks like this abcd,efgh,ijkl,mnop abcd,ef... (7 Replies)
Discussion started by: dnat
7 Replies

2. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

3. Shell Programming and Scripting

awk remove line feed

Hi, I've this file: 1, 2, 3, 4, 5, 6, I need to remove the line feed LF every 3 row. 1,2,3, 4,5,6, Thanks in advance, Alfredo (5 Replies)
Discussion started by: alfreale
5 Replies

4. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

5. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies

6. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

7. Shell Programming and Scripting

sed copy column value add to certain area

I have a base file FILE1 with the following data FILE1.dat 21111111110001343 000001004OLFXXX029100020091112 21111111110000060 000001004ODL-CH001000020091112 22222222220000780 000001013OLFXXX006500020091112 23333333330001695 000001039OLFXXX030600020091112 23333333330000111... (2 Replies)
Discussion started by: kshuser
2 Replies

8. Shell Programming and Scripting

replace last form feed with line feed

Hi I have a file with lots of line feeds and form feeds (page break). Need to replace last occurrence of form feed (created by - echo "\f" ) in the file with line feed. Please advise how can i achieve this. TIA Prvn (5 Replies)
Discussion started by: prvnrk
5 Replies

9. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

10. Shell Programming and Scripting

need script-remove line feed

hi all, i have csv file with three comma separated columns i/p file First_Name, Address, Last_Name XXX, "456 New albany \n newyork, Unitedstates \n 45322-33", YYY\n ZZZ, "654 rifle park \n toronto, canada \n 43L-w3b", RRR\n is there any way i can remove \n (newline) from the second... (1 Reply)
Discussion started by: gowrish
1 Replies
Login or Register to Ask a Question