Reputation: 9
I'm looking to print the 2nd and last column values using spaces and dots to separate the data.
My input is
abcds 874598 thd/vb.sbds/jf 48459 com.dat.first.NAME
Required output is
874598 NAME
Upvotes: 0
Views: 99
Reputation: 203189
From your comments it sounds like you want:
$ awk -F '[[:space:]]+|[.]' '{print $2, $NF}' file
874598 NAME
or:
$ awk '{sub(/.*\./,"",$NF); print $2, $NF}' file
874598 NAME
Upvotes: 0
Reputation: 784938
Using read
and pure BASH:
s='abcds 874598 thd/vb.sbds/jf 48459 com.dat.first.NAME'
IFS=$' \t.' read -ra arr <<< "$s"
echo "${arr[1]} ${arr[-1]}"
874598 NAME
Breakdown:
IFS=' .' # sets input field separator as space or dot
read -ra # reads all fields into an array
${arr[1]} # represents 2nd element in array
${arr[-1]} # represents last element in array
Upvotes: 0
Reputation: 85775
Using awk
:
$ awk -F'[. ]' '{print $2, $NF}' file
874598 NAME
The -F
option sets the fields separate which we provide a character class containing a space or a period. Awk spilt each line in the file into fields using the field separator values and stores them in incrementing references, $1
being the first field, $2
the second ect. NF
is the reference to the number of fields in the current record and $NF
is value of the last field.
You can read the command as:
awk # the command; awk takes a script and executes it on every input line
-F'[. ]' # the field separator; break each line up using spaces or periods
'{print $2, $NF}' # the script to execute; print the second and last field on every line
file # the input file to run the script on
Upvotes: 2