How to extract work in line string.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract work in line string.?
# 8  
Old 03-07-2014
You missed one back slash and "$2"
Code:
tail -1 FBOC810G_140305.CSV | awk -F[\"\.] '{print $2}'


Last edited by pravin27; 03-07-2014 at 02:35 AM..
# 9  
Old 03-07-2014
Here is an alternative way to do it just using awk (without tail). Unless your input files are BIG, it is probably faster to let awk read the entire input file than it is to start up another process to run tail.
Code:
#!/bin/ksh
if [ $# -ne 1 ] || [ ! -r "$1" ] || [ ! -f "$1" ]
then    printf 'Usage: %s readable_regular_file\n' "${0##*/}" >&2
        exit 2
fi
awk -F '[.+"]' '
FNR == 1 { f = FILENAME }
{          t = $2;n = $4 }
END {      if(t == "TRAILER") {
                print f, "transfer complete,", n + 0, "lines."
                exit 0
           }
           print f, "transfer incomplete."
           exit 1
}' "$1"

With an input file named complete.txt containing:
Code:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
"TRAILER.1+0000007+1"

and a file named incomplete.txtcontaining the same 1st six lines, but missing the trailer line, running the above script (assuming you name it checker) as:
Code:
./checker complete.txt

produces the output:
Code:
complete.txt transfer complete, 7 lines

with exit code 0, and running the command:
Code:
./checker incomplete.txt

produces the output:
Code:
incomplete.txt transfer incomplete.

with exit code 1.
# 10  
Old 03-07-2014
Pravin27,
I changed the print to $2 and it worked. Not sure why "TRAILER.1+0000007+1" it would be position $2?

Code:
tail -1 FBOC810G_140305.CSV | awk -F[\"\.] '{print $2}'
TRAILER

# 11  
Old 03-07-2014
Sorry .. I have corrected my earlier post . I was taking input as
Code:
echo "TRAILER.1+0000007+1" | awk -F[\"\.] '{print $1}'

instead
Code:
echo '"TRAILER.1+0000007+1"' | awk -F[\"\.] '{print $2}'

Thanks,
Pravin
# 12  
Old 03-07-2014
Quote:
Originally Posted by pone2332
Pravin27,
I changed the print to $2 and it worked. Not sure why "TRAILER.1+0000007+1" it would be position $2?

Code:
tail -1 FBOC810G_140305.CSV | awk -F[\"\.] '{print $2}'
TRAILER

With double quote " and period . as field separators, and the input above: the first field is the empty string before the first double quote, the second field is TRAILER, the third field is 1+0000007+1 and the fourth field is the empty string after the the trailing double quote.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

6. Shell Programming and Scripting

Extract a string from a line

Hiee all Can anyone tell me how to extract a string from a given line. STAPISDK_RELEASE_32_BL012_2011_JAN_25.1597 I want to extract BL012 from above. as 102 keeps on changing i want smthing like that it extract BL and 102 extrct by its own. Thankx guyzz (9 Replies)
Discussion started by: abhijtr
9 Replies

7. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

8. Shell Programming and Scripting

extract string from varying delimiter line

Hi I have lines like this a=1, b=2, c=3, a=1, d=4, e=5, b=225, I need to extract the b=nnn... value. I dont know how many other entries will be before and after it in each line. Ive tried a basic line like awk '/b=/, $NF ~ /,/ ' myfile.txt but I think that it doesnt care which comma it... (5 Replies)
Discussion started by: rebelbuttmunch
5 Replies

9. Shell Programming and Scripting

How to extract a string from a file ignoring new line

Hi, sumdays before i had posted a query with same subject. i got sum great help from great ppl which solved my problem then. But now there is a small problem with the code that i need the experts help upon. for parsing a text like this where $ had been the delimiter between... (3 Replies)
Discussion started by: suresh_kb211
3 Replies

10. Shell Programming and Scripting

how to extract a string value from the given line

Hi I have an input as follows: param1:value1|param2:value2|param3:value3|param4:value4|param5:value5 where, "|" and ":" are delimiters Now suppose, I want to extract the value corresponding to "param4", i.e. "value4" in this case. In case we use awk, I want to use the value in shell... (1 Reply)
Discussion started by: gaurav_1711
1 Replies
Login or Register to Ask a Question