grepping a part of filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grepping a part of filenames
# 1  
Old 10-18-2006
grepping a part of filenames

Hi ,

I have a list of files in a directory and filename format is as follows:
PQ223390
PQ876912
PQ768901
PQ398140 and so on

I want to grep the first four digits of all the files after PQ, into a file.
Ex:
2233
8769
6890
3981 and so on

Can anyone tell me the command?

thankx jazz
# 2  
Old 10-18-2006
Why do you need a grep to do that? You just need to parse the filenames, right ?

Code:
sed -e "s/^..\(....\).*/\1/g"

# 3  
Old 10-18-2006
Another way, ksh builtin:
Code:
while read line; do
echo ${line:2:4}
done < inputfile

# 4  
Old 10-18-2006
Thank You ALL
# 5  
Old 10-18-2006
Quote:
Originally Posted by shereenmotor
Another way, ksh builtin:
Code:
while read line; do
echo ${line:2:4}
done < inputfile

ksh builtin? Did you mean sh builtin?

Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

for line in PQ223390 PQ876912 PQ768901
do
echo ${line:2:4}
done 
[/tmp]$ ./try.ksh 
./try.ksh[6]: : bad substitution
[/tmp]$

# 6  
Old 10-18-2006
Its a ksh builtin. See this what I get:
Code:
#! /bin/ksh

for line in PQ223390 PQ876912 PQ768901
do
echo ${line:2:4}
done

Output:
Code:
$./tmp
2233
8769
7689

Interactive output:
Code:
$echo $0
-ksh
$a=PQ223310
$b=${a:2:4}
$echo $b
2233

If try to run it with sh then I get same error of bad substitution which you are getting:
Code:
$cat tmp
#! /bin/sh

for line in PQ223390 PQ876912 PQ768901
do
echo ${line:2:4}
done
$./tmp
UX:sh (./tmp): ERROR: Bad substitution

I think you need to check path of your ksh if its really /bin/ksh or not. Otherwise, I'm not sure why you get bad substitution error.

Regards,
Tayyab
# 7  
Old 10-18-2006
Interesting. The man pages for ksh dont mention anything of that construct. But yes, the bash man page does.
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 make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

Renaming Filenames by replacing a part

Hi, I have little experience on Shell scripts, I searched the forum but couldn't make out what I want. I want to rename a set of files to a new file name a_b_20100101 c_d_20100101 ....................... ...................... I want to rename the files to a_b_20140101... (5 Replies)
Discussion started by: JaisonJ
5 Replies

3. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

4. Shell Programming and Scripting

Change part of filenames in a bulk way

Hallo! I have generated lots of data file which all having this format: sp*t1overt2*.txt Now I want to change them in this way: sp*t2overt1*.txt The rest of the file names stay unchanged. I know this is kind of routine action in sed or awk, but dont know how! I tried this command: ... (6 Replies)
Discussion started by: forgi
6 Replies

5. Shell Programming and Scripting

Returning only part of a line when grepping

I want to grep out a part of a snort rule based on the SID given, but all i want as the output is the part in the quotes after the msg: An example line looks something like this: alert tcp any any -> 127.0.0.1 any (msg:"Example Message"; classtype:Example; sid:123456;) I would want it to... (7 Replies)
Discussion started by: riott
7 Replies

6. Shell Programming and Scripting

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like: the errors I'm looking for with other errors: Alerts Password Incorrect Login Error Another Error Another Error 2 Other Info or, just the errors I need to parse for: Alerts Password Incorrect ... (9 Replies)
Discussion started by: elinenbe
9 Replies

7. Shell Programming and Scripting

comparing part of header with part of detailed records.

Hi there, I am lil confused with the following issue. I have a File, which has the following header: IMSHRATE_043008_101016 a sample detailed record is :9820101 A982005000CAVG030108000000000000010169000MAR 2008 9820102 MAR 2008 D030108 ... (1 Reply)
Discussion started by: cmaroju
1 Replies

8. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

9. Shell Programming and Scripting

Grepping for filenames containing value in specific segment within file

I am trying to grep for filenames containing a specific value within a particular segment. The lines containing the segment I'm looking through reads like "HL^1^^1^1", "10^9^9^0", and "HL^11^4^8^1". I would like to find the data that contains only the number nine after the third caret where the... (4 Replies)
Discussion started by: HLee1981
4 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question