Bash Retrieve Beginning of file name and sort question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Retrieve Beginning of file name and sort question
# 8  
Old 10-18-2014
That script if fixed would only loop over the files in the current directory.

Try that and see if it does it.
Code:
find . -name "file*_*" | sort -t_ -k2 -g

# 9  
Old 10-18-2014
Quote:
Originally Posted by Aia
That script if fixed would only loop over the files in the current directory.

Try that and see if it does it.
Code:
find . -name "file*_*" | sort -t_ -k2 -g

Yea I want it to loop over the files in the current directory and then sort it so can I put
Code:
ls *_* | sort -t_ -k2 -g

into the loop? Or should I put it after
# 10  
Old 10-18-2014
Neither! It was intended as a one-liner at the command line, on the current directory.
That's why I gave you the find command that would interact over every directory below the current.

That piece of shell script you posted in post #1 would not do what you want. At most, it would only loop through the files in the cwd, no directory, which ls *_* | sort -t_ -k2 -g does already on its own.
# 11  
Old 10-18-2014
Quote:
Originally Posted by Aia
Neither! It was intended as a one-liner at the command line, on the current directory.
That's why I gave you the find command that would interact over every directory below the current.

That piece of shell script you posted in post #1 would not do what you want. At most, it would only loop through the files in the cwd, no directory, which ls *_* | sort -t_ -k2 -g does already on its own.
Oh! Thank you!

---------- Post updated at 10:25 PM ---------- Previous update was at 10:17 PM ----------

Quote:
Originally Posted by Aia
Neither! It was intended as a one-liner at the command line, on the current directory.
That's why I gave you the find command that would interact over every directory below the current.

That piece of shell script you posted in post #1 would not do what you want. At most, it would only loop through the files in the cwd, no directory, which ls *_* | sort -t_ -k2 -g does already on its own.
Last thing, what if the number after the _ is not the same? How would I sort based on the number before the _. I tried -k1 but that did not work.
# 12  
Old 10-19-2014
Sorting using first field at position five numerically
Code:
ls *_* | sort -t_ -k1.5n


Last edited by Aia; 10-19-2014 at 01:54 AM.. Reason: removed the -r out of ls
# 13  
Old 10-19-2014
Quote:
Originally Posted by Aia
Sorting using first field at position five numerically
Code:
ls -r *_* | sort -t_ -k1.5n

Aia,
I don't understand how the ls -r option helps with this problem??? Why do you want to reverse the order of the filenames printed by ls when you're going to use sort to perform a numeric sort starting on the 5th character of the filenames?

totoro125,
Your requirements are extremely vague:
  1. Are you looking for files in a single directory, or in a directory hierarchy?
  2. If in a directory hierarchy, do you want the directory name included in the output, or do you just want to see the final component of the pathnames?
  3. Do the numbers you want to sort always contain exactly five digits (as in your examples)?
  4. Does the string before the digits you want to sort always consist of exactly four non-numeric characters (as in your examples)?
  5. Is the string before the digits you want to sort always file as in your examples, or does it vary from file to file?
# 14  
Old 10-19-2014
Oh, did not noticed that. That's a mistype. It should be only ls like in previous posts. I edited it.

Last edited by Aia; 10-19-2014 at 02:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort file with certain criteria (bash)?

I am running a command that is part of a script and this is what I am getting when it is sorted by the command: command: ls /tmp/test/*NDMP*.z /tmp/test/CARS-GOLD-NET_CHROMJOB-01-XZ-ARCHIVE-NDMP.z /tmp/test/CARS-GOLD-NET_CHROMJOB-01-XZ-NDMP.z... (2 Replies)
Discussion started by: newbie2010
2 Replies

2. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. Shell Programming and Scripting

Beginning awk question

I have a report that I'd like to print columns headers on. The code appears as follows: gawk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'|gawk '{ printf "%-78s%-30s%-30s%-30s\n", $1,$2, $3, $4 }' |awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' report_1020I am trying to make... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

How do a distinct from a file using sort uniq in bash?

I have an output file .dat. From this file i have to do a distinct of the ID using the sort uniq command in bash script. How can i do it? i found : sort -u ${FILEOUT_DAT} but i don't think is my solution because the id isn't specified.. is there other solution? (7 Replies)
Discussion started by: punticci
7 Replies

5. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

Monitoring a file - Basic Bash Question

*This is not homework I am new to UNIX and want to try this Monitoring a file demo* *If this is the wrong forum please move it - im new to the forums* $1 = the file to be monitored $2 = the time for the file to sleep If the file gets changed (using -nt) it will send my username mail saying... (2 Replies)
Discussion started by: Nolan-
2 Replies

8. Shell Programming and Scripting

bash script to sort a txt file

I am writing a script to write to and a sort txt file. After I sort the file I want to add 2 to each line of the file. My script thus far is #!/bin/bash cat > /ramdisk/home/stux/unsortedints.out COUNT=0 FILE =/ramdisk/home/stux/unsortedints.out for i in {1..100} do NUMBER = $ echo $NUMBER... (3 Replies)
Discussion started by: puttyirc
3 Replies

9. Shell Programming and Scripting

Simple Bash Read File question

Hello all, I am trying to write a simple script that will parse through a text/properties file and check a couple of if statements. Here is what I have so far: FILENAME=$1 while read line do echo $line done < $FILENAME When I call ./simple.sh testfile.txt I recieve a file or... (7 Replies)
Discussion started by: lamagra
7 Replies

10. Shell Programming and Scripting

Sort (bash command)

I did a search on this, and found lots on SORT but no answer to my question. I have a C program that fetches all of our users from Netware, and I have that it makes a file that I later include in a html as a select tag drop-down menu. Here is what 1 line looks like: <option... (5 Replies)
Discussion started by: booboo
5 Replies
Login or Register to Ask a Question