List the files after sorting based on file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List the files after sorting based on file content
# 1  
Old 11-17-2016
List the files after sorting based on file content

Hi,

I have two pipe separated files as below:

Code:
head -3 file1.txt
"HD"|"Nov 11 2016  4:08AM"|"0000000018"
"DT"|"240350264"|"56432"
"DT"|"240350264"|"56432"

Code:
head -3 file2.txt
"HD"|"Nov 15 2016  2:18AM"|"0000000019"
"DT"|"240350264"|"56432"
"DT"|"240350264"|"56432"

I want to list the files based on increasing order of column 3 in first line.
(Column 3 is the file version, and I need to list them in increasing order)

I am able to get the file version using below command:

Code:
for FILE_NAME in `ls *txt`
do 
    FILE_VERSION=$(awk -F'|' '$1=="\"HD\""{gsub(/"/,"",$3);print $3}' $FILE_NAME)
echo $FILE_NAME $FILE_VERSION
done

Output:
Code:
file1.txt 0000000018
file2.txt 0000000019

Is there a way to sort this and list?

P.S. Once I get the list, I will loop the files one by one for processing


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-17-2016 at 03:54 AM.. Reason: Added CODE tags.
# 2  
Old 11-17-2016
How about
Code:
awk -F'|' '$1=="\"HD\"" {gsub(/"/,"",$3); print FILENAME, $3}' *.txt | sort -k2

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-17-2016
how about this ?

same code of @Rudic.. but just adding exit to read only the first line on all files

Code:
for file in *.txt; do awk -F'|' '$1=="\"HD\"" {gsub(/"/,"",$3); print FILENAME, $3;exit}' ${file}; done | sort -k2

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 11-17-2016
Thanks, It gives below output:

Code:
file2.txt 0000000018
file1.txt 0000000019

So I just taking the first field alone:
for file in *.txt; do awk -F'|' '$1=="\"HD\"" {gsub(/"/,"",$3); print FILENAME, $3;exit}' ${file}; done | sort -k2 | awk '{print $1}'

Now, please help me how to loop with this. I need to open the files and do processing with it.
# 5  
Old 11-17-2016
I'd be VERY surprised if any of the proposals yielded THAT output.

In using the output for a loop, where are you stuck?
# 6  
Old 11-17-2016
I got it:

Code:
myarray=`for file in *.txt; do awk -F'|' '$1=="\"HD\"" {gsub(/"/,"",$3); print FILENAME, $3;exit}' ${file}; done | sort -k2 | awk '{print $1}'`
for i in ${myarray[@]}
 do
 echo $i
done

# 7  
Old 11-17-2016
OK, should work. How about
Code:
awk -F'|' '$1=="\"HD\"" {gsub(/"/,"",$3); print FILENAME, $3}' *.txt | sort -k2 | while read FN DUMMY; do echo "$FN"; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find list of missing files based on the file format?

Hi All, In the file names we have dates. Based on the file format given by the user, if any file is not existed for a particular date with in a given interval we should consider that file is missing. I have the below files in the directory /bin/daily/voda_files. ... (9 Replies)
Discussion started by: nalu
9 Replies

2. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

3. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

4. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

5. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

6. Shell Programming and Scripting

[SOLVED] Sorting into new files based on column

Hi, I have the following file, I need to sort it based on a column and write to different output files based on this column request_guid iso_country_cd address_data response_time 32895901-d17f-414c-ac93-3e7e0f5ec240 AND BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND;... (1 Reply)
Discussion started by: ramky79
1 Replies

7. Shell Programming and Scripting

Sorting content of file

hi ladies and gents: can you give me a command to sort content of file and save it to the file itself: file1 roy@emerson.com joy@emerson.com irish@emerson.com output would be file1 on same directory: file1: irish@emerson.com joy@emerson.com roy@emerson.com (6 Replies)
Discussion started by: linuxgeek
6 Replies

8. Shell Programming and Scripting

KSH: Opening Files based on a file list

I'd like to grep files for key words using korn shell, and compile the actual contents (not just file name) of those files that contain a combination of those grepped key words into one repository file for reference. However, I'm stuck at the combining part. Here's what I have thus far: egrep... (5 Replies)
Discussion started by: drumminfool91
5 Replies

9. Shell Programming and Scripting

create diffrent files based on other file and parameters list

I would like ot create shell script/ bash to create diffrent files based on a file and parameters list. Here is the detail example: I have a textfile and four static parameter files (having ‘?'). mainfile.txt has below records (this count may be more than 50) A200001 A200101 B200001... (9 Replies)
Discussion started by: raghav525
9 Replies

10. UNIX for Dummies Questions & Answers

sorting file content on columns

guys i have a question: i'd like to sort files (as many I want) in columns so to visualize them one near the other...so let's say i have just 2 files: FILE1 John Mary Bridget FILE2 Anne Robert Mark i would like to obtain: John Anne Mary Robert Bridget ... (2 Replies)
Discussion started by: marshmallow
2 Replies
Login or Register to Ask a Question