Reading only first record from the multipe directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading only first record from the multipe directories
# 1  
Old 01-19-2011
Reading only first record from the multipe directories

Hi All,

I have a requirement, I had a parent directory Land under that we have sub directories
Yesterday,
Today and
Tommorrow

And we have a file test.txt under the above directories Yesterday, Today and Tommorrow

The data in the file test.txt under Yesterday folder is
Header|2011-12-01
DATA|TOTAL1
DATA|TOTAL2
EOF

The data in the file test.txt under Today folder is
Header|2011-13-01
DATA|TOTAL3
DATA|TOTAL4
EOF

The data in the file test.txt under Tommorrow folder is
Header|2011-14-01
DATA|TOTAL5
DATA|TOTAL6
EOF

I want to get only the 1st records after removing header records from the files like below

DATA|TOTAL1
DATA|TOTAL3
DATA|TOTAL5

I used below command

I used below command awk -F"|" 'BEGIN{IGNORECASE=1;} $1~/DATA/' Land/*/test.txt
I'm getting all the records and the data is as shown below

DATA|TOTAL1
DATA|TOTAL2
DATA|TOTAL3
DATA|TOTAL4
DATA|TOTAL5
DATA|TOTAL6


Thanks,
Raju
# 2  
Old 01-19-2011
One way.

Code:
head -2 /Land/Yesterday/test.txt | tail -1
head -2 /Land/Today/test.txt | tail -1
head -2 /Land/Tommorrow/test.txt | tail -1


Btw. It's spelt "tomorrow" !
# 3  
Old 01-19-2011
Hi Methyl,



Thanks for the reply, I don't know how many subfolders I will have under Land directory. I gave only 3 subfolders as an example. That is whay I mentioned '*' in my command to get all the Subfolders under Land

awk -F"|" 'BEGIN{IGNORECASE=1;} $1~/DATA/' Land/*/test.txt




Thanks,
Raju
# 4  
Old 01-19-2011
Try...
Code:
awk 'BEGIN{IGNORECASE=1;} $1~/DATA/&&!a[FILENAME]++' Land/*/text.txt

# 5  
Old 01-19-2011
Code:
find . -type f -exec awk 'NR==2' {} \;

# 6  
Old 01-19-2011
More than one way to do this. Another way in addtion to using find command:

Code:
 
ls /Land/*/test.txt | xargs -I{} awk 'NR==2' {}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

2. Shell Programming and Scripting

Reading multiple directories and file from them

Hello I'm making script for Dallas temperature sensors (DS1820). When a sensor is connected, it shows up as a directory in /sys/bus/w1/devices in format 10-xxxxxxx. Inside the directory is a file called w1_slave which holds the temperature in format t=xxxxx. Each sensor has unique... (2 Replies)
Discussion started by: Klipeti
2 Replies

3. Shell Programming and Scripting

reading filename in nested directories

Hi all, I have some directory structure as $ls -ltr drwxr-xr-x 1 mscprod us_msc 512 May 10 08:34 650 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 652 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 640 drwxr-xr-x 1 mscprod us_msc512 May 10 09:13 654 $ cd 650/ $ls fileabc.root and within... (7 Replies)
Discussion started by: emily
7 Replies

4. Shell Programming and Scripting

Parsing issue while reading excel having 3000 record

Hi Need urgent help (2 Replies)
Discussion started by: premp26
2 Replies

5. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

6. Shell Programming and Scripting

Reading a file (one record) in a SHL script

I am trying to read a file in a shl script (only one record) and stored in a variable file_number I got the following read -u $BANNER_HOME/xxxxxxx/misc/EFTSQL.dat file_number file_number2 = $file_number + 1 echo $file_number2 > $BANNER_HOME/xxxxxx/misc/EFTSQL.dat EOF It is not working... (2 Replies)
Discussion started by: rechever
2 Replies

7. Shell Programming and Scripting

problem in reading a record from the file

Hi Guys, I need to check whether the last column is RP, If so, then i have to second column and pass it to a select statement as sonid and fetch the value to a variable and run it. This has to be done till the last column is RW. value Fatherid sonid topid ... (8 Replies)
Discussion started by: mac4rfree
8 Replies

8. Shell Programming and Scripting

While loop reading a record

Hi, I am trying to create a while loop that will do the following: INFILE= list of new records that need to be added after last previous record while read record do find the last record processed create list of new records output to a file echo "$record">> $NEWFILE done ... (9 Replies)
Discussion started by: shortyball24
9 Replies

9. UNIX for Dummies Questions & Answers

Clean directories by reading from file

I have a file in following format directory1=/out/log purgedays1=4 extn1=log,out,txt directory2=/clean/log purgedays2=4 extn2=log,out now i need need to create a script that reads this file and cleans all the files with the given extn from the given directory. The catch here is that... (2 Replies)
Discussion started by: max_payne1234
2 Replies

10. Shell Programming and Scripting

Need help with complicated script (reading directories, extracting data)

Hi, people. I was searching script tutorials/examples and found this forum, and thought to ask for help, because there seems to be many wise people here. (Try to bare with me, I'm a Windows user, so this stuff is somewhat strange to me, OK? :rolleyes: ) Anyways, I need to create a script that... (3 Replies)
Discussion started by: Gill Bates
3 Replies
Login or Register to Ask a Question