Parsing a linux file and formatting it.


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Parsing a linux file and formatting it.
# 1  
Old 01-28-2011
Parsing a linux file and formatting it.

Hi, I have a linux file that has data like this..
Code:
REQUEST_ID|text^Ctext^Ctext^C
REQUEST_ID|text^Ctext^C
REQUEST_ID|
REQUEST_ID|
REQUEST_ID|text^Ctext^Ctext^Ctext^Ctext^Ctext^C....

Where ever I see a ^C character, I need to copy the corresponding REQUEST_ID and that part of the text to a new line. so my destination file should look like this..
Code:
REQUEST_ID|text
REQUEST_ID|text
REQUEST_ID|text

.
.
How should I do this? I have no idea how to do this because I have no knowledge in unix or linux so far..

---------- Post updated at 01:50 PM ---------- Previous update was at 11:49 AM ----------

hi, please someone post reply. I need help quickly. I guess it could be done using sed or awk. but no idea how to do. Please let me know how this could be done using sed or awk

Last edited by Scott; 01-29-2011 at 11:08 AM.. Reason: Please use code tags
# 2  
Old 01-28-2011
Narrative: Get rid of empty lines, remove any trailing ^C, branch conditionally to next line to clear t flag, substitute to make a leading line of the request id and first text, replacing the request id on the second line before the second text. If that substitute fired, print the first line, remove it from the buffer and try the substitute again. If not, branch to end, which prints the buffer.
Code:
sed '
  /|$/d
  s/^C$//
  t n
  :n
  s/^\(\([^|]*|\)[^^C]*\)^C/\1\
\2/
  t p
  b
  :p
  P
  s/.*\n//
  t n
 '

# 3  
Old 01-28-2011
hi, thanks for the reply, actually, i did not understand the full command. I am sorry!
But i tried to give this command on the command line.. it did not work. I would appreciate in your further help in better understand this before doing, so that I can do it correctly.
Please correct me if i am wrong some where.

"sed '/|$/d" this is for removing empty lines (i was thinking sed '/^$/d')

"s/^C$//" this is for removing trailing control C s.

I did not understand the rest of the command
"s/^\(\([^|]*|\)[^^C]*\)^C/\1\"

While typing this command, do I have to type the "^" character or ^C character. Confused! Please help..
# 4  
Old 01-28-2011
The ^C's have to be real cntrl-C characters,
a line with no text was not showing in output, so it is virtually empty, and

the big substitute says pick up:
  1. starting at the beginning of the line ^
  2. string1 \( +> \1
  3. and string 2 \( +> \2
  4. string 2 runs from beginning of line to first | (not | = [^|], any number = *, then |)
  5. string 1 is longer, runs to but not including first cntrl-C (not ^C, any number)
  6. the first ^C
but lay back down in their place:
  1. string 1 \1
  2. a line feed \linefeed
  3. string 2 \2
If that is possible, then a line with N texts is now a line of 1 text followed by a line of N-1 texts.

Last edited by DGPickett; 01-28-2011 at 05:54 PM..
# 5  
Old 01-28-2011
But that is not working.. i can post some sample data for you..

Code:
|111111111|
|222222222|1292251978 Prad Bu Development^C
|333333333|
|444444444|1294070403 Joe De Pete Keny is currently testing and will let me know shortly.^C1294416965 Joe De Revised the WF tigger and sent back for re-testing.^C1295020527 Joe De No update this week.^C
|555555555|

I am getting the output as
Code:
222222222|1292251978 Prad Bu Development
444444444|1294070403 Joe De Pete Keny is currently testing and will let me know shortly.^C1294416965 Joe De Revised the WF tigger and sent back for re-testing.^C1295020527 Joe De No update this week.

But I need the output as this..
Code:
111111111|
222222222|1292251978 Prad Bu Development
333333333|
444444444|1294070403 Joe De Pete Keny is currently testing and will let me know shortly.
444444444|1294416965 Joe De Revised the WF tigger and sent back for re-testing.
444444444|1295020527 Joe De No update this week.
555555555|

Please help! Thanks for all your patience..
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-28-2011 at 07:14 PM.. Reason: code tags, please!
# 6  
Old 01-28-2011
Assuming '^C' is actually a SINGLE character (CTRL-C) - not 2 characters...

nawk -f char.awk myFile

char.awk:
Code:
BEGIN {
  FS=OFS="|"
  ctrlC=sprintf("%c", 003)
}
{
   sub("^[|]","")
   sub(ctrlC "$","")
   while (n=index($0,ctrlC))
      $0=substr($0,1,n-1) ORS $1 OFS substr($0,n+1)
   print
}

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 01-28-2011
Hi, Can you please explain me the code? When I wrote this code, and give the nawk command on command line it is saying "command not found"
The code doesn't seem to be unix or linux code! I am not sure but I think "sprintf" is c or c++ code (sorry if I am wrong). Its not working! Can we do this using shell scripting? Control C is a single character. It can be typed on the command line by pressing control+v first followed by control+c. Just FYI, I am working on linux platform.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing syslog from Linux

Hello, I'm facing problem to extract fields from below syslog : logver=56 idseq=63256900099118326 itime=1563205190 devid=FG-5KDTB18800138 devname=LAL-C1-FGT-03 vd=USER date=2019-07-15 time=18:39:49 logid="0000000013" type="traffic" subtype="forward" level="notice" eventtime=1563205189... (17 Replies)
Discussion started by: arm
17 Replies

2. Shell Programming and Scripting

Table like formatting in Linux

Dear experts, I need bit help in formatting .. I have csv file file , i will read that file by passing one column value as input parameter and display header row and corresponding row for that parameter. I have shell script like this: #!/bin/bash #key_word_I_am_looking_for=$1 #awk... (16 Replies)
Discussion started by: onenessboy
16 Replies

3. Shell Programming and Scripting

Specific string parsing in Linux/UNIX

Hi, I have a string which can be completely unstructred. I am looking to parse out values within that String. Here is an example <Random Strings> String1=<some number a> String2=<some number b> String3=<some number c> Satish=<some number d> String4=<some number e> I only want to parse out... (1 Reply)
Discussion started by: satishrao
1 Replies

4. UNIX for Dummies Questions & Answers

Formatting data in a raw file by using another mapping file

Hi All, i have a requirement where i need to format the input RAW file ( which is CSV) by using another mapping file(also CSV file). basically i am getting feed file with dynamic headers by using mapping file (in that target field is mapped with source filed) i have to convert the raw file into... (6 Replies)
Discussion started by: ravi4informatic
6 Replies

5. Shell Programming and Scripting

Formatting a file

Hi Experts I have a file which looks as follows acttest /report/fieldsinf/acttest/LIVE/acttest Chris New teatin1 /report/fieldsinf/eatin1/TEST/teatin1 Chris New eatin1 /report/fieldsinf/eatin1/LIVE/eatin1 Chris New tbectest ... (7 Replies)
Discussion started by: maverick_here
7 Replies

6. Shell Programming and Scripting

Help - Parsing data in XML in Linux

Hi, I have an XML file in Linux and it contains a long string of characters. The last part of the file is like ....... ....... ....... CAD</MarketDescription></InvestorTransaction></AdvisorAccount></DivisionAdvisor></Division>... (3 Replies)
Discussion started by: naveed
3 Replies

7. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

8. UNIX for Dummies Questions & Answers

Parsing linux commands through FTP

Hi Techies, I have made a shell script which stores the output of it in a text file. then i wanted to fetch that text file using windows scheduler in my windows xp desktop which i did successfully using the below mentioned ftp .bat file : @echo off @echo ftp_user>ftp_test.scr @echo... (0 Replies)
Discussion started by: gemnian.g
0 Replies

9. Solaris

Linux partitioned disk mounted on OSOL without formatting

Hello and Merry Christmas... Quick question after tireless search around the web. Description: I have a WD My book world edition II that met an untimely death. However the 2 SATA disks inside seem to be working just fine. Want to add either one of them to my Solaris Desktop. Since I... (5 Replies)
Discussion started by: michnmi
5 Replies

10. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies
Login or Register to Ask a Question