Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script help

Status
Not open for further replies.

mdarsot

Instructor
Apr 13, 2006
86
CA
I have a files like below.


24-TT-000001.temp
< file content >
id year (put in just for description purpose)
24 2005 data
24 2006 data


24-TT-000002.temp
< file content >
id year (put in just for description purpose)
25 2002 data
25 2003 data

What i want to do is go thru list of files and extract file content + file name into another file.


For example the output will look like

id year/part of filename
24 2005/000001
24 2006/000001
25 2002/000002
25 2003/000002


Thanks for your help.



 
Hi Feherke

Thanks for you reply. I dont have gawk or Mawk utility. Also can you please suggest something which can be doable at on multiple files via list.
 
I was doing something like this but then i am not getting resule in same line it is scattered all over.

for file in `cat list`

do

setid=`cat $file | awk '{print $1}'`
year=`cat $file | awk '{print$2}'`
filename=`ls $file`
echo "setid/t$serial\t$filename" >> test.ser

done
 
Perhaps something like this ?
for file in `cat list`
do awk '{print $1"\t"$2"\t"FILENAME}' $file
done > test.ser

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV for the answer it works. However it list the entire filename instead of just part of filename.

Forexample you script does like this.

24 2005/24-TT-000001.temp
24 2006/24-TT-000001.temp
25 2002/24-TT-000001.temp
25 2003/24-TT-000001.temp


however i just want part of filename (stuff between - and . of filename)

24 2005/000001
24 2006/000001
25 2002/000002
25 2003/000002
 
With my starting point and what feherke gave you, you have all the necessary stuff for solving your issue.
 
Thanks PHV for you response

But isn't Feherke using gawk and Mawk which i dont have. I tried doing his way am not getting my answer. Anyhow thanks for your help.
 
Hi

While my [tt]awk[/tt] code works with [tt]mawk[/tt], probably it works with any [tt]awk[/tt] version. Sorry for the unclear explanation.

This one just for my relaxation, I do not think you have [tt]bash[/tt].
Code:
while read n; do
  m=${n##*-}
  m=${m%.*}
  while read -a l; do
    echo "${l[1]} ${l[2]}/${m}"
  done < "$n"
done < /input/file_with_file_list.txt
Tested with [tt]bash[/tt].

Feherke.
 
I think korn does not understand the line while read -a l from feherke's post. I have modified his code below. Here is the version which works on korn

Code:
while read n; do
  m=${n##*-}
  m=${m%.*}
  while read first second third; do
    echo "$first $second/${m}"
  done < "$n"
done < /input/list
 
HI Dstxaix

Thanks for you response. Can you please explain above script also how would i be able to run it with list of files.

 
Before trying this script, you need to list all your temp files into a list file by running the command
Code:
ls *.temp > /input/list

The above command will list all files and redirect to a file named "list" in a directory named "inbound".

Then you can execute the script from my previous post.

The first loop in the script basically takes all files from "list" and takes the value between the second "-" and "." You can take a look at parameter substitution on any Unix shell programming book.

The second loop reads each temp file and reads the first, second and third fields on each line of the file. Then it appends the "/" and the extracted value from the filename.

Hope this helps.
 
Thank you

Works great.

One more question if you want to run let say a sed command on any of the fild let say field second how would you do in your script.

For example $second field needs to go thru further processing like pipe it thur sed command.


 
Thanks i figured it out.

While doing output with
echo "$first $second/${m}"

The above command gives me right output with "/" forward slash.

However if i use "\" back slash insted it does not work. it output $m instead of its value.

If i put a space beside \ slash it works. how can this be possible. Whats the fix.

 
echo "$first $second\\${m}"

You really should read your shell man page.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
HI

I tried reading Parameter substituion pages thru google. I cant find much of explanation for stuff like this
m=${n##*-}
m=${m%.*}


can you please explain what this is doing. I have already solved my problem but wanted to know how it works instead of just taking the solution.

THanks
 
Straight from my ksh man page:
Code:
${parameter#pattern}
${parameter##pattern}
       If the shell pattern matches the beginning of the value of
       parameter, then the value of this substitution is the
       value of the parameter with the matched portion deleted;
       otherwise the value of this parameter is substituted. In
       the first form the smallest matching pattern is deleted
       and in the second form the largest matching pattern is
       deleted.
${parameter%pattern}
${parameter%%pattern}
       If the shell pattern matches the end of the value of
       parameter, then the value of this substitution is the
       value of the parameter with the matched part deleted;
       otherwise the value of parameter is substituted. In the
       first form the smallest matching pattern is deleted and in
       the second form the largest matching pattern is deleted.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top