regex matches from lines in file

Hello,

I try to script something (bash-script) and can not find a way to get and store a match into a variable from a line in a file. grep isn't useful as the matches are not returned - just colored. I can't get 'expr' to work for me. Is it necessary to use a perl-script with regex instead?

example:

reg='\d{2}:\d{2}\s[A-Za-z0-9.]'

strings to match are lines in a file named 'text.out'

thanks in advance..

can u provide the input and the expected output

for example there are columns for location, time and name in the file
the result should just contain time + name or just the names

LOC TIME NAME
nyc 10:30 Roger
la 06:45 Peter

EDIT:

guess I can use awk for this problem.. I'm now reading a tutorial about this..

I found a solution with 'awk'.

result=$(awk 'BEGIN { FS = " " } ; { print $3 }' dbfile.txt)
echo "$(result)"
 
$ cat test.txt
LOC TIME NAME
nyc 10:30 Roger
la 06:45 Peter

 
$ nawk 'NR>1{print $2,$3}' test.txt
10:30 Roger
06:45 Peter

Thank you - looks more simple. another command I need to learn :slight_smile:

is there also a simple way to get all indices from a column on?
e.g. from column 3 to last column..

I tried something like the following but doesn't work :frowning:

res=$(awk 'BEGIN { FS = " " } ; { for(i=5;i<12;i++) { print $$i ; if ($$i = "") i=12 } }' ${resFile})
 
awk 'BEGIN{FS=" "}{for(i=3;i<=NF;i++){print $i}' ${resFile}
1 Like

thx..