prevent newline in cut command - linux

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)}'

Related

Bash tries to execute commands in heredoc

I am trying to write a simple bash script that will print a multiline output to another file. I am doing it through heredoc format:
#!/bin/sh
echo "Hello!"
cat <<EOF > ~/Desktop/what.txt
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
EOF
I was expecting to see a file in my desktop with these contents:
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
But instead, I am seeing these as the contents of my what.txt file:
a=
b=
Somehow, even though it is part of a heredoc, bash is trying to execute it line by line. How do I prevent this, and print the contents to the file as it is?
Quote EOF so that bash takes inputs literally:
cat <<'EOF' > what.txt
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
EOF
Also start using $() for command substitution instead of old and problematic ``.

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.

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

Escaping backslash in AWK

I'm trying to understand why the command below doesn't work (output is empty):
echo 'aaa\tbbb' | awk -F '\\t' '{print $2}'
I would expect the output to be 'bbb'.
Interestingly this works (output is 'bbb'):
echo 'aaa\tbbb' | awk -F 't' '{print $2}'
And this works as well (ouptut is 'tbbb'):
echo 'aaa\tbbb' | awk -F '\\' '{print $2}'
It looks as if \\\t is read as backslash followed by tab instead of escaped backslash followed by t.
Is there a proper way to write this command?
You need to tell echo to interpret backslash escapes. Try:
$ echo -e 'aaa\tbbb' | awk -F '\t' '{print $2}'
bbb
man echo would tell:
-e enable interpretation of backslash escapes

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

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

Resources