how to pass variable to awk in bash for parsing value [duplicate] - bash

This question already has an answer here:
awk: fatal: cannot open file `' for reading (No such file or directory)
(1 answer)
Closed 2 years ago.
Using awk when I try to parse the last folder I get error "No such file or directory". How do I pass variable to awk
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName=$(awk -F/ '{print $NF}' $CacheLocation)

awk does not work on strings, it expects the input as file or stdin
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$(awk -F/ '{print $NF}' <<<"$CacheLocation" )"
In this special case, it might be easier to use basename
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$( basename "$CacheLocation" )"

Related

bin sh script - "(" unexpected [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
What is wrong with this script?
#!/bin/sh
for file in *.p; do awk -f get_p.awk "$file" > "$file"er; done
awk -f phase-comp.awk file1 file.per # výpočet fází
paste <(awk '{print $1}' output1) <(awk '{print $2}' file1) > out
The error is:
5: skript: Syntax error: "(" unexpected
When I write command separately to terminal. It works well. Thank you
The problem is your shebang:
#!/bin/sh
It means this script is meant to be interpreted by sh, but your script uses process substitution which is a bash-specific feature. So, you should change it to:
#!/bin/bash
to make your script work.

How to only read the last line from a text file [duplicate]

This question already has answers here:
How to read the last line of a text file into a variable using Bash? [closed]
(2 answers)
Print the last line of a file, from the CLI
(5 answers)
Closed 4 years ago.
I am working on a tool project. I need to grab the last line from a file & assign into a variable. This is what I have tried:
line=$(head -n $NF input_file)
echo $line
Maybe I could read the file in reverse then use
line=$(head -n $1 input_file)
echo $line
Any ideas are welcome.
Use tail ;)
line=$(tail -n 1 input_file)
echo $line
Combination of tac and awk here. Benefit in this approach could be we need NOT to read complete Input_file in it.
tac Input_file | awk '{print;exit}'
With sed or awk :
sed -n '$p' file
sed '$!d' file
awk 'END{print}' file
However, tail is still the right tool to do the job.

Get the extension of file [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 7 years ago.
I have files with "multiple" extension , for better manipulation I would like to create new folder for each last extension but first I need to retrieve the last extension.
Just for example lets assume i have file called info.tar.tbz2 how could I get "tbz2" ?
One way that comes to my mind is using cut -d "." but in this case I would need to specify -f parameter of the last column, which I don't know how to achieve.
What is the fastest way to do it?
You may use awk,
awk -F. '{print $NF}' file
or
sed,
$ echo 'info.tar.tbz2' | awk -F. '{print $NF}'
tbz2
$ echo 'info.tar.tbz2' | sed 's/.*\.//'
tbz2

Extract Information From File Name in Bash [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
Closed 7 years ago.
Suppose I have a file with a name ABC_DE_FGHI_10_JK_LMN.csv. I want to extract the ID from the file-name i.e. 10 with the help of ID position and file-name separator. I have following two inputs
File-name_ID_Position=4; [since 10 is at fourth position in file-name]
File-name_Delimiter="_";
Here ID can be numeric or alpha-numeric. So how extract the 10 from above file with the help of above two inputs. How to achieve this in bash?
Instead of writing a regex in bash, I would do it with awk:
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F_ -v pos=4 '{print $pos}'
or if you want the dot to also be a delimiter (requires GNU awk):
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F'[_.]' -v pos=4 '{print $pos}'

How to pass variable to awk [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

Resources