awk help: how to pull phrase and one column from line above?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk help: how to pull phrase and one column from line above?
# 1  
Old 09-27-2013
awk help: how to pull phrase and one column from line above?

Hi everyone,

Here's my awk statement so far:
awk '/TOTAL TYPE:/{print x;print};{x=$0}' file1 >file2

'file1' has too much proprietary data in it to include here, so let's go with the output from code above. It looks like this:

    123456       JAMES T KIRK                    D          31       S50  S90          2013/10/31         36
                                                 TOTAL TYPE: 1897
    654321       MR SPOCK                        P           1                 L45     2013/10/31         48
                                                 TOTAL TYPE:   37
    555111       BEN KENOBI                      V          13                 L70     2013/10/31         36
                                                 TOTAL TYPE:  446

What I really want to do is search for the phrase 'TOTAL TYPE', print it and the number afer the colon, and then also print the value from the line above that only occurs in column 50-51 (either a D, P, or V in this example). Spacing might be off a bit for this file example, but the field I want is consistently in 50-51. Doesn't matter to me if I have to make more than one output file to achieve result.

Clear as mud, eh? Thanks a bunch.
# 2  
Old 09-27-2013
Do you mean something like:
awk '
        printf("%s %s %s %s\n", $1, $2, $3, s)
{       s = substr($0, 50, 2)
}' file1 > file2

which produces:

from your sample input. Note that you said you want the last field on printed lines to be the data in columns 50 and 51 of the previous line. Is that always the 3rd field on the line? If so you can simplify the script a little bit by changing:
{       s = substr($0, 50, 2)

{       s = $3

If you want to run this script on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

Last edited by Don Cragun; 09-27-2013 at 06:24 PM.. Reason: Fix typo.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-27-2013
Yes! That's it. Unfortunately, the column value I want isn't always the $3 field when the name has a middle initial. James T Kirk, for example, puts the value I want in $5.

Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Phrase txt file row to column

Hi Guys, I have one Big txt file and i what to phrase specific part as below. Input :- Event Event { recordLength 160118, recordType 411, eventId 3102118, INTERNAL_PER_RO_ME_TA { EVVXX_TIMESTAMP_HOUR 16, EVVXX_TIMESTAMP_MINUTE 15, EVVXX_TIMESTAMP_SECOND 3, ... (6 Replies)
Discussion started by: pareshkp
6 Replies

2. Shell Programming and Scripting

How to find a phrase and pull all lines that follow until the phrase occurs again?

I want to burst a report by using the page number value in the report header. Each section starts with *PAGE NO:* 1 Each section might have several pages, but the next section always starts back at 1. So I want to find the "*PAGE NO:* 1" value and pull all lines that follow until "*PAGE NO:* 1"... (4 Replies)
Discussion started by: Scottie1954
4 Replies

3. UNIX for Dummies Questions & Answers

How to match on phrase at beginning of line and specific columns?

Here is my file: 700 7912345678910 61234567891234567891 700 8012345678910 61234567891234567891 I want to pull all lines that begin with '700' only if columns 11-12 are '79'. My code so far only pulls the '79', not the whole line: grep ^700 file1 | cut -c 11,12 |... (7 Replies)
Discussion started by: Scottie1954
7 Replies

4. Shell Programming and Scripting

Awk next line as column

Hi, This forum rocks. I think this might be an easy thing, but since I am new to awk, please help me. input: x y z 1 a b c 2 d e f 3 g h i 7 output: x y z 1 a b c 2 d e f 3 (8 Replies)
Discussion started by: jacobs.smith
8 Replies

5. Shell Programming and Scripting

simple sed question - replace from phrase to end of line

I have the following line an in input file I want to digest with sed and simple replace the bold part with a variable defined in my bash script. I can do this in several sed operations but I know there must be a way to do it in a single sed line. What is the syntax? Line in file:... (1 Reply)
Discussion started by: graysky
1 Replies

6. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

7. Shell Programming and Scripting

awk : Remove column1 and last column in a line

Hi All, How to remove col1 and last column in a line. Please suggest some awk stuffs. Input col1 col2 col3 col4 col1 col2 col3 col4 col5 col1 col2 col3 col4 col1 col2 col3 Output Processing col2 col3 ... Processing col2 col3 col4 ... Processing col2 col3 ... Processing... (5 Replies)
Discussion started by: k_manimuthu
5 Replies

8. Shell Programming and Scripting

Extracting line matching a phrase and then the next lines after it

Hi all, I was wondering if someone could tell me a way to extract from a file lines where you search for a phrase and then also extract the next X lines after it (i.e. take a block of text from the file)? Example { id=123 time=10:00:00 date=12/12/09 { ........ ... (6 Replies)
Discussion started by: muay_tb
6 Replies

9. Shell Programming and Scripting

awk convert from line to column

i have an output like this : 012008 25760883 022008 12273095 032007 10103 032008 10115642 042007 20952798 but i would like to have it like this 012008,25760883 022008,12273095 032007,10103 032008,10115642 042007,20952798 (4 Replies)
Discussion started by: jarmouda
4 Replies

10. Shell Programming and Scripting

line to column using awk

hi, i'm a newbie and this is my first post here. 'hope all of you fellow members are doing fine. so here is my first thread to ask for help on how to use awk language to do this task. i have a file to process and after a series of other awk commands and shell scripts i managed to convert the... (11 Replies)
Discussion started by: genix2008
11 Replies
Login or Register to Ask a Question

Featured Tech Videos