0

I am writing a bash/awk script to process hundreds of files under one directory. They all have name suffix of "localprefs". The purpose is to extract two values from each file (they are quoted by ""). I also want to use the same file name, but without the name suffix.

Here is what I did so far:

#!/bin/bash

for file in *  # Traverse all the files in current directory.
   read -r name < <(awk ' $name=substr(FILENAME,1,length(FILENAME)-10a) END {print name}' $file) #get the file name without suffix and pass to bash. PROBLEM TO SOLVE!
   echo $name # verify if passing works.
   do
   awk  'BEGIN { FS = "\""} {print $2}' $file #this one works fine to extract two values I want.
   done

exit 0

I could use

awk '{print substr(FILENAME,1,length(FILENAME)-10)}' to extract the file name without suffix, but I am stuck on how to pass that to bash as a variable which I will use as output file name (I read through all the posts on this subject here, but maybe I am dumb none of them works for me).

If anyone can shed a light on this, especially the line starts with "read", you are really appreciated.

Many thanks.

2 Answers 2

2

Try this one:

#!/bin/bash

dir="/path/to/directory"

for file in "$dir"/*localprefs; do
    name=${file%localprefs}  ## Or if it has a .: name=${file%.localprefs}
    name=${name##*/}  ## To exclude the dir part.
    echo "$name"
    awk 'BEGIN { FS = "\""} {print $2}' "$file"  ## I think you could also use cut: cut -f 2 -d '"' "$file"
done

exit 0
Sign up to request clarification or add additional context in comments.

6 Comments

it works like charm. Thank you so much! I will use yours as my template to beatify the output.
#!/bin/bash dir="/path/to/dir" diro="/path/to/dirout" for file in "$dir"/*localprefs; do name=${file%localprefs} ## Or if it has a .: name=${file%.localprefs} name=${name##*/}; ## To exclude the dir part. touch "$diro"/"$name" awk 'BEGIN { FS = "\""} {print $2}' "$file" >>"$diro"/"$name" done exit 0
How can I write the for loop to get the name first and create the file with touch, then run the awk part to put two lines into the file? See above. It is very close but it has two duplicates in the output file
@user46292 Your script seems the right way to do it. What is the error? And does awk produce two lines always? Also would we assume that $diro exists? Or you have to create it?
Per your suggestion, it now produces only two lines. Thanks again.
|
1

To just take sbase name, you don't even need awk:

for file in * ; do
   name="${file%.*}"
   etc
done

3 Comments

Here is what I got with your suggestion:line 12: syntax error near unexpected token FILE=awk '{print substr(FILENAME,1,length(FILENAME)-10)}'' line 12: FILE=awk '{print substr(FILENAME,1,length(FILENAME)-10)}''
No, no, no. Use $()
Edited - it was force of habit to use backticks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.