LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2007, 08:56 AM   #1
sandeepsudeep
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
Parsing text file


I have a text file which reads like this. I need to manipulate its content using sed/awk.

inputfile
================================

AS_CLI/Subs/service> get David
Service pack name
==================
A1
A2
A3


AS_CLI/Subs/service> get Randy
X1
X2
X3

.
.
.
================================================
My output file should look like this..

detail David A1;
detail David A2;
detail David A3;

detail Randy X1;
detail Randy X2;
detail Randy X3;

==========================================
 
Old 10-02-2007, 02:28 PM   #2
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 11.10
Posts: 334

Rep: Reputation: 30
Try looking at the following

Hi there - Try looking over this thread:

http://www.linuxquestions.org/questi...d.php?t=586394

I ran into similar problems regarding using SED to manipulate the contents of a text file - In the thread you will find working SED scripts as well as a bash script that made it possible for me save time making multiple changes to a whole bunch of files.

Hope this is of help to you!
 
Old 10-03-2007, 12:43 AM   #3
sandeepsudeep
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
parsing text file

I am still not able to understand how to go about my parsing text file query. I am very new to sed/awk. Can some one help?
 
Old 10-03-2007, 05:18 AM   #4
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 11.10
Posts: 334

Rep: Reputation: 30
A little explanation

This is a walkthrough one of the simple scripts mentioned in the earlier thread.

for file in *.html
do
cp $file $file.bak &&
sed 's/Copyright 1999-2005 - All rights reserved/Copyright 1999-2007 - All rights reserved/g' $file.bak >$file
done


EXPLANATION LINE BY LINE
**************************************************************************
for file in *.html <--- meaning repeat the following for every file with an html ending (is essentially a txt file ending with html)

do <--- Loop indication

cp $file $file.bak && <--- create backup of all files to a backup version called .bak AND (continues on next line)

sed 's/THE TEXT YOU WANT TO CHANGE/THE TEXT YOU WISH IT TO BE/g' $file.bak >$file <--- here you indicate what to change and what to change it into the / is the delimiter - it could be # if you had lines with slashes in it etc.

done <--- continue until all above has been executed

**************************************************************************

The bash script in the link I supplied you with earlier curtesy of jozybaof is searching for an exact substring and replaces it with another.

I would strongly suggest you read the discussion in the mentioned thread as I believe if you are indeed trying to change something in a text file you will have no trouble doing so by following the advice.

There is also a tutorial on SED at http://sed.sourceforge.net/grabbag/tutorials/ - Even going as far as teaching you to use SED to solve the Towers of Hanoi.

Towers of Hanoi - From Wikipedia.org BEGIN QUOTE: "The Tower of Hanoi or Towers of Hanoi is a mathematical game or puzzle. It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape.

The objective of the game is to move the entire stack to another peg, obeying the following rules: * Only one disk may be moved at a time. * Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg. * No disk may be placed on top of a smaller disk. END QUOTE

And if your mastery of SED comes as far as that level let me be the first to congratulate you!

Perhaps I have not quite understood your dilemma correctly - Hope this has been of assistance.
 
Old 10-03-2007, 06:00 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
A single liner in awk:
Code:
awk 'BEGIN {name=""} /> get /{name=$NF} /^[AX][0-9][0-9]*$/{if(name!=""){ print "detail " name " " $1}}' input
Or, reformatted to make the awk program more readable:
Code:
BEGIN {
    name=""
} 

/> get / {
    name=$NF
} 

/^[AX][0-9][0-9]*$/ {
    if (name!="") {
        print "detail " name " " $1
    }
}
It works like this:
  • At the start of the program, make sure the variable name holds an empty string. This isn't really necessary, but it's instructive to be explicit about it.
  • Then, for any line where you see "> get ", set the name to the last field on the line (NF is a variable which hold the number of fields on a line. If you have 5 fields on a line, $NF will evaluate to the value of the fifth field, and is the same as putting $5 in this case).
  • Finally, if any line starts with an A or X, and then has numbers after that, print an output line with "detail " then the current value of name, and then the first field in the current line.
 
Old 10-09-2007, 06:04 AM   #6
sandeepsudeep
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
parsing text file

Thanks Mathew. I understood the awk script you send and I have one more query.

BEGIN {
name=""
}

/> get / {
name=$NF
}

/^[AX][0-9][0-9]*$/ {
if (name!="") {
print "detail " name " " $1
}
}


### We have done this ^[AX][0-9][0-9]*$ based upon the fact that there are numbers in the records like X1, x2, X3.. What if it is not there.. for example

AS_cLI> get david
rto
gdghhhgh
fsdfsd
AS_cLI> get randy
ffsd
dfdfsdfsdfs
fsdffsdf
=================
and I need output

detail david rto
detail david gdghhhgh
.
.
.
.
.
.
.

How to go about that? How do I select them...

sandeep sudeep
 
Old 10-09-2007, 06:11 AM   #7
sandeepsudeep
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
parsing text file

Here is my input file

====
running >>> q all
running >>> Subs
running >>> Service
running >>> Service
running >>> get BroadsoftIndia
Service Pack Name
=================
Basic-Pack
gaurav-pack
gj

3 entries found.
running >>> !sleep 2
running >>> q all
running >>> Subs
running >>> Service
running >>> Service
running >>> get sp_gtest
Service Pack Name
=================
rot
rtrtrt
0 entry
====================

Output file should be like

detail BroadsoftIndia Basic-Pack
detail BroadsoftIndia gaurav-pack
detail BroadsoftIndia gj
detail sp_gtest rot
detail sp_gtest rtrtrt
 
Old 10-09-2007, 06:34 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Please use [CODE] tags when posting code examples or data files which should be displayed in fixed-width fonts - it helps readability.

Code:
#!/usr/bin/awk -f

BEGIN {
    name=""
}

/> get / {
    name=$NF
}

! /^(running|Service Pack Name|=|[0-9][0-9]* entr)/ {
    if (name!="") {
        print "detail " name " " $0
    }
}
I just modified the pattern which says what lines to output on to this:
Code:
! /^(running|Service Pack Name|=|[0-9][0-9]* entr)/
Which means: execute the following block of code if the input line does not match a line beginning with (^) "running" or (|) "Service Pack Name" or "=" or one or more digits followed by " entr".

I also changed the print line to print $0 - the whole line - rather than just $1 - the first field on the line. This is just in case you have some package name which contains spaces.

There is a potential problem here... if you have a package name with "> get " in the name, it will be treated as a new name, not a package name. You should check that this is not the case.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing text file in php ohcarol Programming 1 08-25-2006 10:18 AM
I need help parsing text from a text file rsmccain Linux - General 2 01-05-2006 03:43 PM
Need help parsing text file scilec Programming 5 12-02-2004 02:00 PM
need help parsing text file airman99 Linux - General 2 10-08-2004 10:09 PM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 08:13 PM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration