How to read nth word from delimited text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read nth word from delimited text file?
# 1  
Old 06-11-2012
How to read nth word from delimited text file?

Hi i am new in scripting
how i can get 2 elements from first line of delimited txt file in shell scripts.


AA~101010~0~AB~8000~ABC0~
BB~101011~0~BC~8000~ABC~
CC~101012~0~CD~8000~ABC0~
DD~101013~0~AB~8000~ABC~
AA~101014~0~BC~8000~ABC0~
CC~101015~0~CD~8000~ABC~

can anyone plse help?


Thanks in advance!!!!
# 2  
Old 06-11-2012
Code:
awk -F'~' 'NR==1{print $2,$4}' input.txt

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-11-2012
try
Code:
cat input.txt | cut -d "~" -f2,4


Last edited by Franklin52; 06-11-2012 at 04:39 AM.. Reason: Please use code tags
This User Gave Thanks to expert For This Post:
# 4  
Old 06-11-2012
use exit, so that other lines will not be read by awk

Code:
awk -F'~' 'NR==1{print $2,$4;exit}' input.txt

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

2. Shell Programming and Scripting

Find for line with not null values at nth place in pipe delimited file

Hi, I am trying to find the lines in a pipe delimited file where 11th column has not null values. Any help is appreciated. Need help asap please. thanks in advance. (3 Replies)
Discussion started by: manikms
3 Replies

3. Programming

How can I read complete word from text file?

how can I know that the word in test file end at this point .... I have an Idea to search on ' ' , '\n' or '\0' but don't know what function can store the string before those characters .. help please ! (3 Replies)
Discussion started by: fwrlfo
3 Replies

4. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

5. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

6. UNIX for Dummies Questions & Answers

how to read the second word of a text file

Folks, how to read the second word of the first line from a text file. Text file does not have any delimiters in the line and has words at random locations. Basically the text file is a log and i want to capture a number that is in second position. Appreciate your help Venu (1 Reply)
Discussion started by: venu
1 Replies

7. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

8. Shell Programming and Scripting

how to read delimited text into array

I am trying to parse a string using delimited char into array. BNAME=B10,B20,B30 B10.Q=X B20.Q=Y B30.Q=Z I need to parsethe BNAME into array, then i will loop through array to execute command using these variables. like: for i in $array do qload array array.Q # execute command: qload B10... (3 Replies)
Discussion started by: adars1
3 Replies

9. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question