Hi, been about 10 years since I've scripted, so very rusty and could use some quick help.
I have a file that contains data like such:
folder1 jondoe owner janedoe reader joeshmo none
folder2 jondoe none janedoe none joeshmo owner
folder3 jondoe owner
folder4 janedoe owner joeshmo reader
Want to search for janedoe for each line of the file and print the next column. So, expecting output like:
folder1 janedoe reader
folder2 janedoe none
folder 4 janedoe owner
Much thanks in advance.
Drew
One way would be :
sed -n 's/ .*\(janedoe [^ ]*\).*/ \1/p' infile
jaduks
December 11, 2009, 11:19pm
3
Using awk:
$ awk '
{ for(j=0;j<=NF;j++)
if ( $j == "janedoe" )
print $1,$j,$(j+1)}
' infile
bash
while read -r line
do
case "$line" in
*janedoe*)
next=${line##*janedoe }
next=${next%% *}
first=${line%% *}
echo "$first janedoe $next"
esac
done <"file"
output
# ./shell.sh
folder1 janedoe reader
folder2 janedoe none
folder4 janedoe owner
while(<DATA>){
print $1,"\n" if /(.*janedoe\s*[^ ]*)/;
}
__DATA__
folder1 jondoe owner janedoe reader joeshmo none
folder2 jondoe none janedoe none joeshmo owner
folder3 jondoe owner
folder4 janedoe owner joeshmo reader
Much appreciated everyone!
A shorter one.
awk '$2 ~ /janedoe/ { print $1" "$2" "$3 }' file