user1691717
user1691717

Reputation: 223

Extracting a list of files and creating a new file containing this list

I am a researcher and my skill in Unix commands is limited. I am currently dealing with a folder containing about 1000 files and I have to extract some filenames from this folder and create another file (configuration file) containing these filenames.

Basically, the folder has filenames in the following format :

1_Apple_A_someword.txt 
1_Apple_B_someword.txt 
2_Apple_A_someword.txt 
2_Apple_B_someword.txt 
3_Apple_A_someword.txt 
3_Apple_B_someword.txt

and so on up until

1000_Apple_A_someword.txt
1000_Apple_B_someword.txt

I just want to extract out all files which have "Apple_A" in them. Also, I want to create another file which has 'labels' (Unix variables) for each of these "Apple_A" files whose values are the names of the files. Also, the 'labels' are part of the filenames (everything up until the word "Apple") For example,

1_Apple=1_Apple_A_someword.txt
2_Apple=2_Apple_A_someword.txt
3_Apple=3_Apple_A_someword.txt

and so on...till

1000_Apple=1000_Apple_A_someword.txt

Could you tell me a one-line Unix command that does this ? Maybe using "awk" and "sed"

Upvotes: 0

Views: 1386

Answers (4)

Vijay
Vijay

Reputation: 67291

ls -1|perl -F_ -ane 'if($_=~m/Apple_A/){print $F[0]."_".$F[1]."=".$_}'

Upvotes: 0

potong
potong

Reputation: 58483

This might work for you (GNU sed):

ls -1 | sed '/^\([0-9]\+_Apple\)_A/!d;s//\1=&/'

Upvotes: 1

MeaCulpa
MeaCulpa

Reputation: 911

An awk version, FYI.:

ls -1 | awk -vFS='_' '/Apple_A/ {print $1"_"$2"="$0}'

Upvotes: 0

lhf
lhf

Reputation: 72362

Try

ls *Apple_A* | sed 's/\(\(.*Apple\).*\)$/\2=\1/'

Upvotes: 2

Related Questions