Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How do I separate a portion of a file name to use grep on? Post 302987063 by Scrutinizer on Sunday 4th of December 2016 10:26:45 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 07:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy