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
# 1  
Old 10-18-2014
Bash Retrieve Beginning of file name and sort question

I am trying to look through a directory where the file names are like this: stringnumber_string, and I want to see if I can take the first part of the name and sort it by the number. I tried this:
Code:
for i in *_; do
	if[-f "$i"]; then
	fileNum=${i%_*};
	echo $fileNum
	fi
done

but it gives me an error. How can I fix this?
And I was trying to use this to sort it:
sort -t _ -k 2 -g ./

Is there a way to put these together to work?
Thanks for any help.
# 2  
Old 10-18-2014
Could you post a representation of:
Code:
ls *_*

or file names that you say
and a copy and paste of the error?

Last edited by Aia; 10-18-2014 at 09:56 PM.. Reason: error
# 3  
Old 10-18-2014
Quote:
Originally Posted by Aia
Could you post a representation of:
Code:
ls *_*

or file names that you say
and a copy and paste of the error?
example file names: file54353_04, file34444_04, file12345_04

error: syntax error near unexpected token `then'

I tried taking away the then and fi, but then it gives me this error: if[-f *_]: command not found
# 4  
Old 10-18-2014
Does this do what you want?
Code:
ls *_* | sort -t_ -k2 -g

The error is due to the test, it should be:

Code:
if [ -f "$i" ]; then

It is required to have spaces after and before `[ and ]'.

---------- Post updated at 07:21 PM ---------- Previous update was at 07:13 PM ----------

Also glob (that is the `*_' in the for loop) are implicitly anchored. Meaning that it is the equivalent of start of string followed by pattern followed by end of string.
*_ means to expand to files that start with any or none sequence of character, followed with an underscored followed by end of string. Which it will only hit names ending in `_'
This User Gave Thanks to Aia For This Post:
# 5  
Old 10-18-2014
Quote:
Originally Posted by Aia
Does this do what you want?
Code:
ls *_* | sort -t_ -k2 -g

The error is due to the test, it should be:

Code:
if [ -f "$i" ]; then

It is required to have spaces after and before `[ and ]'.
Would I have to use both codes? Or do I use them separately?
And thanks that fixed the problem, but it is not searching into my subdirectories so it is not outputting anything.
# 6  
Old 10-18-2014
Only that
Code:
ls *_* | sort -t_ -k2 -g

or
Code:
ls file*_* | sort -t_ -k2 -g

if file correspond to the real "string" portion of the file
# 7  
Old 10-18-2014
Quote:
Originally Posted by Aia
Only that
Code:
ls *_* | sort -t_ -k2 -g

or
Code:
ls file*_* | sort -t_ -k2 -g

if file correspond to the real "string" portion of the file
I mean do I put it in the if statement so I can sort it?
Sorry I am pretty new to unix and bash; trying to learn for my new job coming up.
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