Need help to get the nth field of the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to get the nth field of the variable
# 8  
Old 07-28-2011
Your code is giving me the below output

[e030809@vhldvspspa001 ~]$ echo "SPLASH/tags" | awk -F"/" '{print NF}'
2

its priting the number of fields where I want the name like tag but if I use $2 instead of
NF it it give me tag but the input can vary sometime it is SPLASH/tags and sometime it could be only tags that's why I want only the last field of the input.
# 9  
Old 07-28-2011
Add a $ in front of NF and it will work.

You did not say anything to:
Code:
$> read I; awk -F"[)/]" -v i=$I '$1 == i {print $NF}' infile
46
tags

Also you are still not using code tags, even I asked 2 times for it. You got your 1st infraction.
# 10  
Old 07-28-2011
Hi

You code is correct but my requirement is little bit differ this

read I; awk -F")" -v i=$I '$1 == i {print $2}' temp.txt3

Requirment
after pressing repo. number it store the value in temp.txt3 file in below format so sometime it stores only SBS and sometime store DIB/H2H but if it store
DIB/H2H then I need H2H and if store SBS then I need SBS.
My flie is temp.txt3 after pressing the repo. number which stores the value
cat temp.txt3 (show the content of temp.txt3)
DIB/H2H

Now I want only H2H



my code of the script

repocodebase()
{
echo -en "Enter the REPO number(1,2...):"
read usrInput
count=0
cat /home/e030809/temp.txt2|while read line; do count=`echo $count + 1 | bc`; echo " $count)$line "; done | grep -w $usrInput>/home/e030809/temp.txt3
a=`cat /home/e030809/temp.txt3|sed 's/^[ \t]*//;s/[ \t]*$//'|cut -d ")" -f2`
#a=`cat /home/e030809/temp.txt6|sed 's/[ \t]*$//'|cut -d ")" -f2`
echo $a

cat temp.txt3 store sometime SBS and sometime store DBD/H2H and nothing else

so we cann't use field separator and we don't use to put here number 28 or 29
output of the file is
cat temp.txt3
DBD/H2H

neither 28)DBD/H2H

Thanks
# 11  
Old 07-28-2011
Somehow you are still posting this code:
Code:
read I; awk -F")" -v i=$I '$1 == i {print $2}' temp.txt3

but if there was already this one posted:
Code:
read I; awk -F"[)/]" -v i=$I '$1 == i {print $NF}' infile

I state this now the 2nd or 3rd time and this is the last time. It is also not polite to ignore itkamaraj's question as well it is not ok to still do not post with using code tags after all. You get another infraction for this. I offered you already to explain it again or better, if you did not understand the PM you got, but no response and still posting without.
If you collect 30 infraction points, you will be banned from this forum. Up to you.
# 12  
Old 07-28-2011
Your code was

echo "SPLASH/tags" | awk -F"/" '{print NF}'
so it was giving me the total number of fields
i.e. 2

exact code is

echo "SPLASH/tags" | awk -F"/" '{print $NF}'
whereas this is giving me the nth field
tags

---------- Post updated at 06:18 PM ---------- Previous update was at 06:08 PM ----------

Need to know how we can achieve this
Below is the input I want to add $i in this field shldvgfas001 and the output would be
shldvgfas0$i.tvlport.net.(Output I want).Let you know one thing that is the input will vary because it is the output of hostname command,I just want to add $i in the last of first field (replace last two char. or digit with $i of first field)
of this input

shldvgfas001.tvlport.net (Input)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help changing date format in the nth field

Hi, I have two (2) things that I want to do. First is to change the date format that is in the nth field from MM/DD/YY to YY/MM/DD. Preferably, I wish I know how to make it a 4-digit year but I don't. Problem is I can only assume it is a 20 century Second is somehow know how to figure out... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

3. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

4. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

5. Shell Programming and Scripting

Nth field should have space and rest commas

Hello I was working on a script where the output of my file is 1234 4567 8973 43214 78965 I need the value in below format of this file.The nth field should have space instead of ,(comma) 1234,4567,8973,43214 78965 I tried the code but not working completely xargs <temp_PP.7250... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

6. UNIX for Dummies Questions & Answers

Print Nth to last field

Hey, I'm sure this is answered somewhere but my Googling has turned up nothing. I have a file with data in the following format: <desription of event> at <time and date>The desription of the event is variable length and hence when the list is displayed it is hard to easily see the date (and... (8 Replies)
Discussion started by: RECrerar
8 Replies

7. Shell Programming and Scripting

Awk product of nth field

How do I do the product of nth filed just like sum. For sum I know like awk '{ sum += $12 } END {printf "%.2f\n", sum}' works as initial sum = 0. But for product how do initialize the variable to 1? (2 Replies)
Discussion started by: madhavb
2 Replies

8. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

9. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

10. Shell Programming and Scripting

awk substitute variable value in nth field

I have a large csv file that looks like this: The 3rd field is a unix time stamp that I want to convert to human readable. I wrote a bash script with this code: IFS=$',' cat $1 | while read ID user DATE text flags read; do echo -e "$ID,$user,$(date -d @$DATE),$text,$flags,$read... (3 Replies)
Discussion started by: stumpyuk
3 Replies
Login or Register to Ask a Question