awk extract "Matthew" from mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew - linux

How to extract "Matthew" from "mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew"

echo "mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew"| awk -F ':' '{print $5}'

I find echo "mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew"| cut -d: -f5 simpler.

Or, just let the shell do it (assuming bash)
entry="mtjoseph:6/MKTCzMS/YU.:10974:10060:Matthew"
name="${entry##*:}"
echo "$name" # ==> Matthew

Related

Extracting a string from a string in linux

Extract the value for OWNER in the following:
{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL danielwayne#blah.com}}
To get this string I am using grep on a text file. In all other cases only one value is contained on each line, so they are not an issue.
My problem is removing the text after OWNER but before the }} brackets, leaving me with only the string 'Wayne, Daniel'.
So far I have began looking into writing a for loop to go through the text a character at a time, but I feel there is a more elegant solution then my limited knowledge of unix.
With grep
> cat file
{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL danielwayne#blah.com}}
> grep -Po '(?<=OWNER )[\w, ]*' file
Wayne, Daniel
Try cat file.txt | perl -n -e'/OWNER ([^\}]+)/ && print $1'
You can use this awk:
awk -F '{{|}}' '{sub(/OWNER +/, "", $4); print $4}' file
Wayne, Daniel
Try this. I use cut
INPUT="{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL danielwayne#blah.com}}"
SUBSTRING=`echo $INPUT| cut -d' ' -f3`
SUBSTRING2=`echo $INPUT| cut -d',' -f2`
SUBSTRING2=`echo $SUBSTRING2| cut -d'}' -f1`
echo $SUBSTRING$SUBSTRING2
maybe is not the most elegant way but works.

prevent newline in cut command

Is it possible to cut a string without a line break?
printf 'test.test' prints the test.test without a newline.
But if I cut the output with printf 'test.test' | cut -d. -f1 there's a newline behind test.
There are many ways. In addition to isedev and fedorqui's answers, you could also do:
perl -ne '/^([^.]+)/ && print $1' <<< "test.test"
cut -d. -f1 <<< "test.test" | tr -d $'\n'
cut -d. -f1 <<< "test.test" | perl -pe 's/\n//'
while read -d. i; do printf "%s" "$i"; done <<< "test.test
No that I know. man cut is quite short and doesn't reflect anything similar.
Instead, you can provide the cut output to printf with a here-string, so that the new line issue depends again on printf:
printf '%s' $(cut -d. -f1 <<< "test.test")
If you don't have to use cut, you can achieve the same result with awk:
printf 'test.test' | awk -F. '{printf($1)}'

Bash script: Read text after characters

I'd like to read the text after characters in a file.
For example:
MPlayer-2013-08-30-i486|MPlayer|2013-08-30-i486||Multimedia;video|4508K||MPlayer-2013-08-30-i486.pet|+ffmpeg|mplayer video player|slackware|14.0||
I'd like to read the version of the program (in the third box):
2013-08-30-i486
How I can do this in my bash script?
This is pretty easily done with cut:
echo 'MPlayer-2013-08-30-i486|MPlayer|2013-08-30-i486||Multimedia;video|4508K||MPlayer-2013-08-30-i486.pet|+ffmpeg|mplayer video player|slackware|14.0||' | cut -d '|' -f 3
2013-08-30-i486
which will split on | and choose the 3rd field.
Using BASH regex:
s='MPlayer-2013-08-30-i486|MPlayer|2013-08-30-i486||Multimedia;video|4508K||MPlayer-2013-08-30-i486.pet|+ffmpeg|mplayer video player|slackware|14.0||'
[[ "$s" =~ MPlayer-([^|]+) ]] && echo "${BASH_REMATCH[1]}"
2013-08-30-i486
Using awk:
awk -F 'MPlayer-|\\|' '{print $2}' <<< "$s"
2013-08-30-i486
To grab 3rd field using awk:
awk -F '\\|' '{print $3}' <<< "$s"
2013-08-30-i486
This is simple to do in AWK:
$ awk -F'|' '{print $3}' file
2013-08-30-i486
It seems that the same data is repeated in several places, so I assume that they are all OK to use...In the above line, the input is being split into fields on the | character and the third field is being printed. The same thing will happen for every line of input.
Through grep,
$ grep -oP 'MPlayer-\K[^|.]*(?=\|)' file
2013-08-30-i486
Through sed,
$ echo 'MPlayer-2013-08-30-i486|MPlayer|2013-08-30-i486||Multimedia;video|4508K||MPlayer-2013-08-30-i486.pet|+ffmpeg|mplayer video player|slackware|14.0||' | sed -r 's/^[^|]+\|[^|]+\|([^|]+).*$/\1/'
2013-08-30-i486
Using read (all shells):
IFS='|' read __ __ VERSION __ < file
echo "$VERSION"
Another using read -a and Bash arrays:
IFS='|' read -a FIELDS < file
echo "${FIELDS[2]}"
Output:
2013-08-30-i486
The read built-in will be most efficient for a single line:
IFS="|" read __ __ version __ <<< "$line"
although if you are processing a file full of such lines with
while IFS="|" read __ __ version __; do
# do something with $version
done < file
it might be more efficient to use cut:
while read version; do
# do something with $version
done < <(cut -d'|' -f3 file)
or awk:
awk -F'|' '{ # do something with $3 }' file

can not use unix $variable in awk command [duplicate]

This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
I have following variable set in my unix environment. If i try to use it in awk command its not working but the same command is working when i dont use $b variable
$b="NEW"
when i try following command it is not working
echo "$a" | tr [a-z] [A-Z] |awk -v RS=, '/TABLE/&&/CREATE/&&/`echo ${b}`/{print $NF}'
But, if i replace the $b value to NEW as below its working
echo "$a" | tr [a-z] [A-Z] |awk -v RS=, '/TABLE/&&/CREATE/&&/NEW/{print $NF}'
You cannot use a bash var inside awk like that. Instead, use:
echo "$a" | tr [a-z] [A-Z] | awk -v RS=, -v myvar=$b '/TABLE/&&/CREATE/&& $0~myvar {print $NF}'
See an example:
$ var="hello"
$ awk -v text=$var 'BEGIN{print text}'
hello
Also, to me it works with tr 'a-z' 'A-Z' instead of tr [a-z] [A-Z]. And based on Mark Setchell suggestion, you can skip it by using the IGNORECASE = 1:
echo "$a" | awk -v RS=, -v myvar=$b 'BEGIN{IGNORECASE=1} /TABLE/&&/CREATE/&& $0~myvar {print $NF}'
Regarding your question:
if i replace the $b value to NEW as below its working
It works because the value of your variable is NEW and what you end up doing is using that in the regex, which is exactly how it is supposed to be done.
about your second question:
can not use unix $variable in awk command
You cannot use shell variables in awk like that. You need to create an awk variable by using -v option and assigning your bash variable.
awk -v awkvar="$bashvar" '/ /{ ... }'
This makes your existing syntax as:
echo "$a" | tr [a-z] [A-Z] | awk -v RS=, -v var="$b" '/TABLE/&&/CREATE/&&/var/{print $NF}'
This again won't work because inside /../ variables are not interpolated, meaning they are considered literally. So, you need to do:
echo "$a" | tr [a-z] [A-Z] |awk -v RS=, -v var="$b" '/TABLE/&&/CREATE/&&$0~var{print $NF}'

linux shell storing path

I want to find the path of folder and store it into a variable
#!/bin/bash
howdy=$(whereis yum.repos.d)
howdy=$howdy"/remi.repo"
echo $howdy
The issue is that when I want to use the variable $howdy , it will output
yum.repos: /etc/yum.repos.d/remi.repo
I want it to be just the path
/etc/yum.repos.d/remi.repo
so I could use it in my code
You can use Parameter Expansion:
${howdy#yum.repos: }
You can erase the first part of the string:
echo ${howdy##*: }
Or you can pipe it through cut -d' ' -f2
you can use awk to format the output:
howdy=$(whereis yum.repos.d| awk '{print $2}')
Full code:
#!/bin/bash
howdy=$(whereis yum.repos.d| awk '{print $2}')
howdy=$howdy"/remi.repo"
echo $howdy
this will output: /etc/yum.repos.d/remi.repo
How about:
howdy=$(find /etc -type f -name remi.repo)
whereis yum.repos.d | awk -F: '{print $2}' will give strip off the first part

Resources