LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-28-2009, 08:33 PM   #1
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Rep: Reputation: 0
Text-processing and loops ...


Quote:
Originally Posted by homey View Post
Good! I charge extra if you appreciate it.


Just Kidding!
How much is an explanation of the command?

I have similiar situation except the string I want is a hostname, so variable in length up to 8 but always followed by a constant in the record. Then later in the same record I need to pull an 'architecture type' that will either be "x64" or "i386".

I'll be looping thru a few records to pull the same info from each and then doing some processing based on these two variables. So how do I construct the loop around each record (line) read?

Last edited by skipdashu; 04-28-2009 at 08:43 PM.
 
Old 04-28-2009, 09:33 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what is your sample text like? and show your output.
 
Old 04-28-2009, 09:47 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Not much to work with ...
Assuming your file looks something like this:
Code:
Is this really hostname1 FIXED  i386 a working
example host2name FIXED  x64 of what the 
guy on LQ is looking 3rdhost FIXED  i386 for after all?
He didn't give us host5 FIXED  x64 too much to work
with, did he? confused4host FIXED  i386
No, not and6host FIXED  x64 really...
This will extract the hostname and the architecture from
the stream
Code:
sed -r 's/.* ([^ ]+) +FIXED +([^ ]+).*/\1 \2/' file

And piping that into the following:
Code:
sed -r 's/.* ([^ ]+) +FIXED +([^ ]+).*/\1 \2/' file |  while read host arch; do echo "host: "$host" arch: "$arch; done 
host: hostname1 arch: i386
host: host2name arch: x64
host: 3rdhost arch: i386
host: host5 arch: x64
host: confused4host arch: i386
host: and6host arch: x64


Cheers,
Tink

P.S.: I've split your post out of the 5 year old zombie ...
 
Old 04-29-2009, 06:57 PM   #4
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Original Poster
Rep: Reputation: 0
parsing clients out of a log file

Quote:
Originally Posted by Tinkster View Post
Not much to work with ...
Assuming your file looks something like this:
Code:
Is this really hostname1 FIXED  i386 a working
example host2name FIXED  x64 of what the 
guy on LQ is looking 3rdhost FIXED  i386 for after all?
He didn't give us host5 FIXED  x64 too much to work
with, did he? confused4host FIXED  i386
No, not and6host FIXED  x64 really...
This will extract the hostname and the architecture from
the stream
Code:
sed -r 's/.* ([^ ]+) +FIXED +([^ ]+).*/\1 \2/' file

And piping that into the following:
Code:
sed -r 's/.* ([^ ]+) +FIXED +([^ ]+).*/\1 \2/' file |  while read host arch; do echo "host: "$host" arch: "$arch; done 
host: hostname1 arch: i386
host: host2name arch: x64
host: 3rdhost arch: i386
host: host5 arch: x64
host: confused4host arch: i386
host: and6host arch: x64


Cheers,
Tink

P.S.: I've split your post out of the 5 year old zombie ...
Tink, Thanx for getting to a current thread and for the answer... I'll be working on this tonight... very shortly. So I'm sure there'll be questions waiting tomorrow ;-) such a noob i r, i r

Somebody ask for the input file format... a typical line looks like:

Code:
Sun Mar 29 00:29:04 CDT 2009 : Added client crunch21 ; IP : 192.168.218.21 ; MAC : 00:1D:7D:96:A9:9A ; CLIENT ROOT : \/diskless\/crunch21 ; architecture : x64
I'm after "crunch21" and "x64" out of this one. I'm trying to construct a loop and do processing based on these to variables out of each line, then loop to get the next line (2 variables) and do the same processing for those two... until I hit EOF.

Below is a fragment of the script I'm trying to write where it got to be mostly notes to myself about what I wanted to do. This is the 2nd script I've ever written and I might be taking on more than I can handle yet.

Thanx again, I'll try to work the code you gave me into this.

Code:
fi
#
# File exists so parse out of it each clients hostname and arch type
echo "Working..."
while read LINE
do

# Parse out all the client names, arch types from $CLIENT_LOG file
# each line looks like:
#Sun Mar 29 00:29:04 CDT 2009 : Added client crunch21 ; IP : 192.168.218.21 ; MAC : 00:1D:7D:96:A9:9A ; CLIENT ROOT : \/diskless\/crunch21 ; #architecture : x64 
# hostname= the string starting after "Added client " and ending before ";".
# arch= the string after "architecture : " and ending before the newline.
#
#
# Then start copying updated config file from /Diskless/master.$ARCH/$PATH_FILE to
# /Diskless/$HOST/$PATH_FILE
#
        echo "$LINE"
        :

done < $CLIENT_LOG
Skip

Last edited by skipdashu; 04-29-2009 at 06:59 PM.
 
Old 04-29-2009, 08:55 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026
This should give you a starting point:
Code:
t='Sun Mar 29 00:29:04 CDT 2009 : Added client crunch21 ; IP : 192.168.218.21 ; MAC : 00:1D:7D:96:A9:9A ; CLIENT ROOT : \/diskless\/crunch21 ; architecture : x64'

echo $t|awk '{ print $10, $27 }'

crunch21 x64
eg http://www.ibm.com/developerworks/library/l-awk1.html
 
Old 04-30-2009, 01:02 AM   #6
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Original Poster
Rep: Reputation: 0
short detour?

OK some I'm still working on this script and decided I'd better work top down... I'm having a problem in a step right before I start parsing the file lines... it's about permissions to execute a cp -vp file1 file2 > logfile. May I post here or where?

Skip
 
Old 04-30-2009, 08:42 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026Reputation: 2026
Maybe start a new one. Incidentally, you may be a newbie, but these are Prog qns, you should probably put them in the Prog forum.
You may well get a faster response as well.
 
Old 04-30-2009, 08:51 PM   #8
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Maybe start a new one. Incidentally, you may be a newbie, but these are Prog qns, you should probably put them in the Prog forum.
You may well get a faster response as well.
OK. Is there a bash or scripting under Linux forum? Didn't see anything under Linux.

Last edited by skipdashu; 04-30-2009 at 09:18 PM.
 
Old 04-30-2009, 09:21 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
If you have permission problems as well as coding, here's just fine,
and our programming forum is quite generic (covers ANY type of coding,
from bash script to .NET on windows ;}).




Cheers,
Tink
 
Old 04-30-2009, 09:35 PM   #10
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Original Poster
Rep: Reputation: 0
copying files in bash, directing output to a log file

OK then I have two questions but I'll put them in two posts... one is about using cp the other refers back to the AWK to parse from earlier.

copying inside my script (lower of the two on my priority list):

This works:
Code:
sudo cp -pv /diskless/master.i386/$PATH_FILE /diskless/master.x64/$PATH_FILE
This doesn't:
Code:
sudo cp -pv /diskless/master.i386/$PATH_FILE /diskless/master.x64/$PATH_FILE >> /var/dotsch_ux/propgt.log
I did set -x and it appears to be telling me I have a permissions error on the log file. At the top of the script I did:
Code:
sudo touch /var/opt/dotsch_ux/propgt.log
The first time I ran it stopped and ask me for the sudo password (I could tell it was the touch asking because it hadn't yet done some "echos" that follow it.

It's not critical but I would like to know how to push the output of the copy verbose to a log file if I really needed it. Or is this whole concept just silly?
 
Old 04-30-2009, 09:51 PM   #11
skipdashu
LQ Newbie
 
Registered: Apr 2009
Location: República de Tejas, Centro
Distribution: Ubunut, Xubuntu, Dotsch/UX
Posts: 19

Original Poster
Rep: Reputation: 0
assigning AWK output to variables - SOLVED

This code:
Code:
# File exists so parse out of it each clients hostname and arch type
echo "Working..."
echo ""
while read LINE
do

# Parse out all the client names, arch types from /var/opt/dotsch_ux/add_diskless_client.log

        echo $LINE| awk '{ print $10, $27 }'
        HOST=$10
	ARCH=$27
	echo $HOST, $ARCH
        :

done < $CLIENT_LOG
Produces this output:
Code:
crunch21 x64
0, 7
crunch22 x64
0, 7
The "crunch21" and "x64" are just what I need. But I need them assigned to variables $HOST and $ARCH as they become part of a pathname for a subsequent copy command.

PS: Tried this also...
Code:
HOST="$LINE| awk '{ print $10 }'"
and this
Code:
ARCH="`$LINE| awk '{ print $27 }'`"
and these
Code:
HOST=`$LINE| awk '{ print $10 }'`
ARCH=$( $LINE| awk '{ print $27 }' )
AND FINALLY... this one worked:
Code:
HOST=`echo $LINE| awk '{ print $10 }'`

Last edited by skipdashu; 05-01-2009 at 12:24 AM. Reason: solved
 
  


Reply

Tags
awk, bash, permissions, variables


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
multi-line text processing pcock Programming 1 03-01-2009 07:31 PM
Basic ASCII text processing alanminor Linux - Software 1 06-05-2007 06:40 AM
text processing, maintaining a log jonhewer Linux - Newbie 9 08-25-2005 02:24 AM
text editingor processing exodist Linux - General 2 11-29-2003 10:24 PM
text processing Gantrep Linux - General 4 02-17-2003 10:37 PM


All times are GMT -5. The time now is 11:20 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration