I haven't found anything that clearly answers my question. Although very close, I think...
I have a file with a line:
# Skipsdata for serienummer 1158
I want to extract the 4 digit number at the end and put it into a variable, this number changes from file to file so I can't just search for "1158". But the "# Skipsdata for serienummer" always remains the same.
I believe that either grep
, sed
or awk
may be the answer but I'm not 100 % clear on their usage.
You can use grep
with the -o
switch, which prints only the matched part instead of the whole line.
Print all numbers at the end of lines from file yourFile
grep -Po '\d+$' yourFile
Print all four digit numbers at the end of lines like described in your question:
grep -Po '^# Skipsdata for serienummer \K\d{4}$' yourFile
-P
enables perl style regexes which support \\d
and especially \\K
.
\\d
matches any digit (0-9). \\d{4}
matches exactly four digits.
\\K
lets grep forget the previously matched part, such that only the part afterwards is printed.
Using Awk
as
numberRequired=$(awk '/# Skipsdata for serienummer/{print $NF}' file)
printf "%s\n" "$numberRequired"
1158
There are multiple ways to find your number. Assuming the input data is in a file called inputfile
:
mynumber=$(sed -n 's/# Skipsdata for serienummer //p' <inputfile)
will print only the number and ignore all the other lines;
mynumber=$(grep '^# Skipsdata for serienummer' inputfile | cut -d ' ' -f 5)
will filter the relevant lines first, then only output the 5th field (the number)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.