I want to extract a specific string to a specific character in a shell script.

Asked 1 years ago, Updated 1 years ago, 62 views

The following log files are available:

aaa1234.234;
bbbb5678.345;
ccc12345.356;

aaa2345.344;
bbbb6789.354;
ccc23456.678;

I want to extract only the number of bbbb from "bbb" to ";" from this file.
There is no string like bbb.
The number of digits in this number is indefinite, and it is also uncertain how many sets of data such as a-c, but
If possible, could you store it in different variables or in one variable, including line breaks?

This is a supplement

ans=`grep-o`bbbb[^;]*`test.txt`

was able to get out of bbbb;
In addition, sed replaces bbbb,

ans=`echo "$ans" | sed-e's /^bbb//'`

was able to extract lines with new lines.
I'd like to put this in a different variable for each line.
If I don't know the number of lines, it will be difficult to separate them.

Supplement 2
It worked by specifying a new line in IFS.
Thank you.

linux shellscript

2022-09-30 21:32

1 Answers

I have received a few answers, and I will summarize them now that they have been resolved.

ans=`awk-F'[;]'$1=="bbb"{print$2}'test.txt`

Or

ans=`grep-o`bbbb[^;]*`test.txt`
an = `echo "$ans" | sed-e's /^bbb//'`

allowed the variable ans to leave only between bbbb 」 and ;; 」 with a new line.

Next,

IFS='
'
for i in $ans;do
    echo$i
done

I could treat it as a different variable by describing the function in echo.
Thank you to everyone who responded.


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.