How do I separate a portion of a file name to use grep on?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How do I separate a portion of a file name to use grep on?
# 1  
Old 12-03-2016
How do I separate a portion of a file name to use grep on?

I'm trying to write a script that takes a file name in the form of Name_Num1_Num2.Extension and I want to separate the name portion and then use grep to see if that name part has any illegal characters in it.

I already have my grep command written and it works, I'm not sure how to separate the Name part from the two _Num1_Num2.Extension, is it possible to just look at just the one part of the file name and perform my command on it?

I also want to do the same with the _Num1_Num2 and the .Extension part with my script to check if they contain illegal characters as well, not sure how to approach that as well.

The reason I want to check each part separately is because each part of the file name has different illegal characters where the name cannot contain anything other than letters, digits and then the number portion should only contain digits.

So in short my question is: is there any way I can check portions of filenames? Or is there a better way to approach this? Thanks.

---------- Post updated at 04:22 PM ---------- Previous update was at 04:00 PM ----------

Never mind, I got a solution by doing the following:

Code:
inputFile="name_1_0.cc"
name="${inputFile%_*_*}"
echo $name
name

# 2  
Old 12-03-2016
Thanks for sharing!
Lets see if others have a better suggestion...
This User Gave Thanks to vbe For This Post:
# 3  
Old 12-03-2016
I also found that the following works as well.
Code:
name=$(echo $inputFile | cut -d'_' -f1)
minor=$(echo $inputFile | cut -d'_' -f3 | cut -d'.' -f1)
major=$(echo $inputFile | cut -d'_' -f2)
ext=$(echo $inputFile | cut -d'.' -f2)

# 4  
Old 12-04-2016
How about - provided you are using bash -
Code:
IFS="_." ARR=( $inputFile )
for IX in ${!ARR[@]}; do echo ${ARR[$IX]}; done
name
1
0
cc

# 5  
Old 12-04-2016
General shell syntax, using unix pattern matching of the case statement to check the filename format, without separating the filename in different parts:
Code:
case $inputFile in
  (*[![:alnum:]]*_*_*.*|*_*[![:digit:]]*_*.*|*_*_*[![:digit:]]*.*|*_*_*.*[![:alnum:]]*)
    echo "filename is wrong"
  ;;
  (*_*_*.*)
    echo "filename is correct"
  ;;
  (*)
    echo "filename is wrong"
  ;;
esac

--
Or bash/ksh93/zsh, same thing, but this time using extended regular expression matching:
Code:
if [[ $inputFile =~ ^[[:alnum:]]+_[[:digit:]]+_[[:digit:]]+\.[[:alnum:]]+$ ]]; then
  echo "filename is correct"
else
  echo "filename is wrong"
fi

--
A quick general (POSIX) way to split the filename in different variables, using a here-document:
Code:
IFS='_.' read name major minor ext << EOF
$inputFile
EOF

Or bash/ksh93/zsh, using a here-string:
Code:
IFS='_.' read name major minor ext <<< "$inputFile"



---
Quote:
Originally Posted by RudiC
How about - provided you are using bash -
Code:
IFS="_." ARR=( $inputFile )
[..]

Note: this will globally change IFS and not local to the array assignment. Also the order in which these two assignments will be executed is not defined, so it is best to use a semicolon or newline to separate them and to make sure the IFS assignment is done before the array assignment and save IFS and restore it later,
Code:
oldIFS=$IFS
IFS="_."
ARR=( $inputFile )
IFS=$oldIFS


or use (bash):
Code:
IFS="_." read -a ARR <<< "$inputFile"

In the latter case IFS does get set local to the read command.

Last edited by Scrutinizer; 12-04-2016 at 11:58 AM..
# 6  
Old 12-05-2016
Hi,

If the name format is same, then you can try something like this..
Code:
inputFile="name_1_0.cc"

Code:
echo $inputfile | cut -d "_" -f1 | xargs | grep -i "charecters"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

2. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

3. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

4. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

5. Shell Programming and Scripting

How to grep a portion of line

Mysql log has something like: I want to grep only the portion "ernie-1328697839.1233158" from each line. How to do this? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

6. UNIX for Dummies Questions & Answers

Grep line to separate column

Hi friends I have a file like this I want output like this probably in excel sheet . Please help Waiting for reply Thanks a lot (14 Replies)
Discussion started by: umapearl
14 Replies

7. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

8. UNIX for Advanced & Expert Users

Grep a portion of the log file

I want to grep a portion of the log file. grepping a particular pattern and including 10 lines before that and after that occurence. grep -n "SomeString Pattern" filename 10 lines before this occurence and 10 lines after that. Please help. Need the simple script not in awk or sed. (9 Replies)
Discussion started by: sainipardeep
9 Replies

9. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

10. Shell Programming and Scripting

Separate a portion of text file into another file

Hi, I have my input as follows : I have given two entries- From system Mon Aug 1 23:52:47 2005 Source !100000006!: Impact !100000005!: High Status ! 7!: New Last Name+!100000001!: First Name+ !100000003!: ... (4 Replies)
Discussion started by: srikanth_ksv
4 Replies
Login or Register to Ask a Question