How do I find a number in between numbers and colons? - bash

I'm trying to get the numbers that's in between two colons and other numbers in a file.
Example:
1234:12345678:1234
1234:12345678:1234
1234:12345678:1234
I want the output to show all of the 12345678's and nothing else.
Like this:
12345678
12345678
12345678
I achieved this using:
egrep -o "[0-9]{8}" file
Problem is that I need a different solution than egrep -o (awk or sed?)
I searched and tried a couple of things but without succes.
Any help would be appreciated!

If the "number" is always 2nd column, you could do with awk:
awk -F: '{print $2}' file

For the awk solution,
awk -F: '{print $2}' file
Or simply use cut to do that,
cut -d: -f2 file

would this work?
awk -F':' '{print $2}' test > test results.txt

Related

Match a length of string in column using awk

I'm trying to extract the column which matches the date pattern like: YYYY-MM-DD, but didn't understand the difference between below commands
Working command
echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/) print $1}'
Not working command
echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9]{d}-[0-9]{2}-[0-9]{2}$/)print $1}'
Can someone explain me why it's happening, am i missing a granular thing?
You could try following and let me know if this helps you.
echo 1,2,3,4,2015-12-34 | awk --re-interval -F, '{if($5~/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)print $1}'
OR
echo 1,2,3,4,2015-12-34 | awk --re-interval -F, 'match($NF,/[0-9]{4}-[0-9]{2}-[0-9]{2}/){print $1}'
NOTE: My awk version is OLD so I am using --re-interval you could remove it in case you have recent awk version.

grep serial numbers not starting with specific prefix

I have this file (serials.txt) containing serial numbers:
S/N:175-1915011190
S/N:244-1920023447
S/N:335-1920101144
S/N:244-1920101149
Using grep or similar tool I want to select all serials NOT starting with '244'
I'm able to select all the '244' with grep -Eo '244-[0-9]*' serials.txt but I want the opposite.
Something like grep -Eo '(^244)-[0-9]*' serials.txt
The output should be (without S/N:)
175-1915011190
335-1920101144
Following awk may help you in same.
awk '!/S\/N:244/' Input_file
EDIT: Above code will give complete line as output if you need starting from serial number to till end in output then following may help you.
awk -F':' '!/S\/N:244/{print $2}' Input_file
EDIT2: Adding a sed solution too here for same.
sed -n '/:244/d;s/.*://;p' Input_file
The -v option on grep would be helpful here, and then cut to remove the leading cruft:
grep -v ':244-' serials.txt | cut -c5-
Here you go, without S/N:
grep -v ':244' serials.txt | cut -d':' -f2
Antigrep for :244, cuts with delimiter : shows field 2.
awk -F':' '$2!~/^244/{print $2}' file

Reading numbers from a text line in bash shell

I'm trying to write a bash shell script, that opens a certain file CATALOG.dat, containing the following lines, made of both characters and numbers:
event_0133_pk.gz
event_0291_pk.gz
event_0298_pk.gz
event_0356_pk.gz
event_0501_pk.gz
What I wanna do is print the numbers (only the numbers) inside a new file NUMBERS.dat, using something like > ./NUMBERS.dat, to get:
0133
0291
0298
0356
0501
My problem is: how do I extract the numbers from the text lines? Is there something to make the script read just the number as a variable, like event_0%d_pk.gz in C/C++?
A grep solution:
grep -oP '[0-9]+' CATALOG.dat >NUMBERS.dat
A sed solution:
sed 's/[^0-9]//g' CATALOG.dat >NUMBERS.dat
And an awk solution:
awk -F"[^0-9]+" '{print $2}' CATALOG.dat >NUMBERS.dat
There are many ways that you can achieve your result. One way would be to use awk:
awk -F_ '{print $2}' CATALOG.dat > NUMBERS.dat
This sets the field separator to an underscore, then prints the second field which contains the numbers.
Awk
awk 'gsub(/[^[:digit:]]/,"")' infile
Bash
while read line; do echo ${line//[!0-9]}; done < infile
tr
tr -cd '[[:digit:]\n]' <infile
You can use grep command to extract the number part.
grep -oP '(?<=_)\d+(?=_)' CATALOG.dat
gives output as
0133
0291
0298
0356
0501
Or
much simply
grep -oP '\d+' CATALOG.dat
You don't need perl mode in grep for this. BREs can do this.
grep -o '[[:digit:]]\+' CATALOG.dat > NUMBERS.dat

command-line with variable, doesn't work

I have a script that uses awk with parameters but I can't find the right syntax to make it functionnal. May be could you help me ?
(under osx, terminal, zsh, command line)
I get a variable who is the path name (it's a awk result)
path_dir="/picture/dir/'
After, I ask this:
awk -F"/" '{print $2}' $path_dir
But it doesn't work. I get:
cant' open file $path_dir
My goal is to do this:
dir_name=awk -F "/" '{print $2}' $path_dir
Then, to use
$dir_name
But first, awk can't read my $path_dir
Any idea ?
Thank you
try this:
dir_name=$(awk -F'/' '{print $2}' <<<$path_dir)
this should assign dir_name with picture
Give a try to:
dir_name=`awk -F "/" '{print $2}' $path_dir`

Get a number out of a string without spaces

I grep this string from a SVG file:
<g transform="translate(0.000000,980.000000) scale(0.100000,-0.100000)"
and I want to get the number 980 within a bash script variable. My problem is that I don't manage to pipe the right expression to get only that number 980 out of the string. I need to add that, of course, the number 980 can be any number from any length (for example, 5967 or 98...).
Could you offer me a solution? I'm stuck there...
Assuming your SVG input file is input.svg. You should try:
cat input.svg | grep translate | sed 's/^.*translate(.*,\(.*\)\..*) scale.*$/\1/'
From my understanding, you can use awk.
grep whatever | awk -F"," '{print $2}' | awk -F"." '{print $1}'
No need for more than one simple command:
awk -F"[,.]" '/your_search_string/{print $3}'
HTH Chris
just bash, I assume you want the 2nd parameter to "translate()"
$ line='<g transform="translate(0.000000,980.000000) scale(0.100000,-0.100000)"'
$ num=${line#*translate*,}; echo $num
980.000000) scale(0.100000,-0.100000)"
$ num=${num%%.*}; echo $num
980
This might work for you:
sed '/^<g transform/!d;s/[^,]*,//;s/[^0-9].*//
The below awk command should work for you.
awk -v FS="[ =(,)]" '{print $5}' "Yourfilename" | awk -v FS="[.]" '{print $1}'

Resources