getting the last parameter through awk & split


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting the last parameter through awk & split
# 1  
Old 09-05-2012
Data getting the last parameter through awk & split

Hi,

I would like to know how to extract and get the value of the last element in an array after split.

Let's say i have an input of
Code:
aaa mykeyword_111 abc/def/ghi/mno
bbb none ttt.aaa.ccc
ccc mykeyword_222 ppp/qqq/rrr/ss

I expect the output to be like this way.
Code:
aaa 111 mno
ccc 222 ss

I am trying to write in such a way below.
Code:
awk '$2 ~/mykeyword/ {
  {split($2, arr, "_")}
  {split($3, arr2, "/") } <---- i wanna get the last element here
  {print $1","arr[2]","arr2[THE_LAST_ELEMENT]}
}' logfile

I tried arr2[NR] or arr2[$NR] but still failed..

Thank you.
# 2  
Old 09-05-2012
Code:
echo 'SOME|DUMMY|DATA'|awk '{n=split($0,a,/[|]/);print "LAST ELEMENT : " a[n]}'
LAST ELEMENT : DATA

Get it?
# 3  
Old 09-05-2012
Another approach:
Code:
awk '$2 ~ /mykeyword/{sub(".*_",x,$2); sub(".*/",x,$NF);print $1, $2, $NF}' file

# 4  
Old 09-05-2012
awk

Hi,

How about this..?

Code:
awk -F'[ _/]' '$2 ~/mykeyword/{print $1","$3","$NF;}' file

awk 'BEGIN{FS="[ _/]";OFS=",";}$2 ~/mykeyword/{print $1,$3,$NF;}' file

Cheers,
Ranga Smilie
# 5  
Old 09-09-2012
Quote:
Originally Posted by elixir_sinari
Code:
echo 'SOME|DUMMY|DATA'|awk '{n=split($0,a,/[|]/);print "LAST ELEMENT : " a[n]}'
LAST ELEMENT : DATA

Get it?
Thanks! never realize that i could get the number of elements in such a way...
Learnt a new thing today! thank you!

Quote:
Originally Posted by Franklin52
Another approach:
Code:
awk '$2 ~ /mykeyword/{sub(".*_",x,$2); sub(".*/",x,$NF);print $1, $2, $NF}' file

It works!
Thank you!
This approach is a bit high level to me... ^^; but learnt a new thing today!
Thank you!

Quote:
Originally Posted by rangarasan
Hi,

How about this..?

Code:
 
awk -F'[ _/]' '$2 ~/mykeyword/{print $1","$3","$NF;}' file
 
awk 'BEGIN{FS="[ _/]";OFS=",";}$2 ~/mykeyword/{print $1,$3,$NF;}' file

Cheers,
Ranga Smilie
Good! I just know taht we could specify multiple FS... thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

2. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

3. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

4. Shell Programming and Scripting

Split into columns based on the parameter and use & as delimiter

Here is my source, i have million lines like this on a file. disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=& drc=&mo=&sid=&lang=EN&loc=JPN I want to split this into columns in order to load in database, anything starts with"&mjv=6" as first... (13 Replies)
Discussion started by: elamurugu
13 Replies

5. Shell Programming and Scripting

split the file based on the 2nd column passing as a parameter

I am unable to spit the file based on the 2nd column passing as a parameter with awk command. Source file: “100”,”customer information”,”10000” “200”,”customer information”,”50000” “300”,”product information”,”40000” script: the command is not allowing to pass the parameters with the awk... (7 Replies)
Discussion started by: number10
7 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

compare & split files

Hi All, I've 1 big file like: cat nid_lec_rej_20090804_merged 10084MOCLEC 0408090061480739nid090804132259.03.148990533 2526716790000008947850036448540401014 R030007150692000 2535502720000000010100036165742685000 R030007150354000 ... (12 Replies)
Discussion started by: ss_ss
12 Replies

8. Shell Programming and Scripting

Array split function & hashes

Hi, If this is the array that is being returned to me: How would I get the values for each of the 3 records? This works for 1 Record: foreach $item (@results) { ($id, $id2, $name, $date, $email) = split(/\|/, $item, 5); print "$name<br>"; } (2 Replies)
Discussion started by: novera
2 Replies

9. Shell Programming and Scripting

Sort & Split records in a file

Hi, I am new to scripting. I need a script to sort and the records in a file and then split them into different files. For example, the file is: H1...................... H2...................... D2.................... D2.................... H1........................... (15 Replies)
Discussion started by: Sunitha_edi82
15 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question