Help awking a 'head -1 file.txt' input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help awking a 'head -1 file.txt' input
# 1  
Old 03-05-2010
Help awking a 'head -1 file.txt' input

Hi there,

my ksh script collects a procstack trace for a particular pid and then greps it by a transaction id to find out the pthread ID:
---------- tid# 1876087 (pthread ID: 4466) ----------

So the pthread ID I want is 4466 in this case, and it is assighed to the variable $pthread. This works fine so far.

After this point, I need to read the first line of a few files whose names are composed by several variables in $HEAD_FILE and a number that it reads from file task2. This currently contains:
32888
So HEAD_FILE will be /logs/eComm_esn_32888.log and the loop will only run once. The first line of this file contains:
1021 2010-03-02 17:06:17 0000-00-00 00:00:00 +0100 00000000 001 003f 0001 09 eComm 32888 893162 4466 /logs/eComm_32888.log 7.8.2.8 [19237] ESN

I want to set a condition that if field number 15 is equal to the pthread ID (4466) it should print the whole line. Otherwise not. AWK has difficulty in reading the first few characters, so I have used sed to replace them with "nothing" (8 dots).
This makes 4466 be in the $14th position now so I can change the condition.

Here´s the relevant part of the script:
========
Code:
## Take a procstack trace of the pid to find the pthread_id
                        procstack $PID > "${OM}_procstack_${PID}.txt"
                        pthread=`cat $OM_procstack_$PID.txt |grep $TID | sed -e 's/^.*ID: //' -e 's/).*//g'`
                        echo "   "
                        echo "------pthread ID corresponding to transaction $TID is $pthread ------" >> $LOG
                        while read line
                           do
                                HEAD_FILE=`head -1 /logs/eComm_${line}.log | sed -e 's/^........//g'`
                                echo $HEAD_FILE
                                awk '$14 == 4466 {print $14, $15, $16}' < $HEAD_FILE
                           done < task2
                fi
        done < tasks

=======

Running this as it is gives me the following:

Code:
2010-03-02 17:06:17 0000-00-00 00:00:00 +0100 00000000 001 003f 0001 09 eComm 32888 893162 4466 /logs/eComm_32888.log 7.8.2.8 [19237] ESN
No such file or directory
./orange.sh[83]: 2010-03-02 17:06:17 0000-00-00 00:00:00 +0100 00000000 001 003f 0001 09 eComm 32888 893162 4466 /logs/eComm_32888.log 7.8.2.8 [19237] ESN: cannot open

=======


So it echoes the value of HEAD_FILE as it understands it now, and the number I am interested in is in 14th position, but I don´t know how to get around the 'cannot open' error to make my condition work.

I only want the 1st line of the file read in the loop which contains the number matching the value of $pthread, obtained a little earlier (4466). Does anyone have any ideas of what I am doing wrong? I think I´ve been staring at this too long..

Thanks in advance!
T.

Last edited by radoulov; 03-05-2010 at 03:03 PM..
# 2  
Old 03-05-2010
Instead of this:

Code:
echo $HEAD_FILE
awk '$14 == 4466 {print $14, $15, $16}' < $HEAD_FILE

Try this:

Code:
echo $HEAD_FILE | awk '$14 == 4466 {print $14, $15, $16}'

Thanks,
Eugene

Last edited by Franklin52; 03-05-2010 at 03:37 PM.. Reason: Please use code tags, thank you
# 3  
Old 03-06-2010
Hi turk541,

Your suggestion worked, thanks! Smilie)

I have tested that if the number is matched it prints, and if it doesn´t, then it doesn´t print.

However, as the 4466 number could change on each run of the script, I have replaced the number for variable $pthread, which is defined a few lines up. Now this line gives me errors:

Code:
awk: Field  is not correct.
 The input line number is 1.
 The source line number is 1.

My line is now:
Code:
echo $HEAD_FILE | awk '$14 == $pthread {print $0}'

I have tried ${pthread} also, but with the same error.. Smilie(

T.

Last edited by Scott; 03-06-2010 at 07:02 AM.. Reason: Code tags please...
# 4  
Old 03-06-2010
Code:
echo $HEAD_FILE | awk '$14 == thr {print $0}' thr="${pthread }"

# 5  
Old 03-06-2010
Hi vgersh99,

your suggestion worked, thanks.

I would never have guessed it cos I can´t see why I have to define and use another variable to compare values when pthread was already defined....I have so much to learn yet.. Smilie/

Thanks again!
T.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

2. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

3. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

4. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

6. Shell Programming and Scripting

Get the input from user and save it as .txt file

Hi friends, I am pretty new to shell scripting, please help me in this Scenario. for example, If I have one file called input.txt once I run the script, 1.It has to delete the old input.txt and create the new input.txt (if old input.txt is not there, no offence, just it has to create a... (2 Replies)
Discussion started by: Padmanabhan
2 Replies

7. Shell Programming and Scripting

cat vs head vs readline get variable from txt file

I have a file with a single filename in it, which I want to assign to a BASH variable, so I've been trying: c=$(head -1 somefile) echo $c which outputs correctly, but them when I do ... somecommand $c it says it can't find the file, is that because it's grabbing the whole line, and... (5 Replies)
Discussion started by: unclecameron
5 Replies

8. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. Shell Programming and Scripting

How to input .txt file into .xls spreadsheet

I need to take the totals from my script and input them into a excel spreadsheet. Right now, I just copy and paste. Is there an easier way? 3906 is the total jobs in ABEND state 4005 is the total jobs in SUCC state 1050 is the total jobs in HOLD state (1 Reply)
Discussion started by: wereyou
1 Replies
Login or Register to Ask a Question