1

I have a file with following text as below

classA = Something
classB = AB1234567
classC = Something more
classD = Something Else

Objective:
Using a shell script, I want to read the text which says AB1234567 from above complete text.

So to start, I can read the second line of the above text using following logic in my shell script:

secondLine=`sed -n '2p' my_file`;
echo $secondLine;

secondLine outputs classB = AB1234567. How do I extract AB1234567 from classB = AB1234567 in my shell script?

Question:
Considering the fact that AB is common in that particular part of the text all the files I deal with, how can I make sed to read all the numbers after AB?

Please note that classB = AB1234567 could end with a space or a newline. And I need to get this into a variable

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28

4 Answers4

3

Try:

sed '2{ s/^classB = \(AB[^ ]*\) *$/\1/;q } ;d' your_fileName
  • 2 is the line number.
    • { open a sed group command.
      • s/ substitute below match
        • ^ is anchor for beginning of the line
        • \(...\) is known a capture group with \1 as its back-reference
        • [^ ]* means any character but not a space
        • \(AB[^ ]*\) capture AB followed by anything until first space seen but not spaces (back-reference is \1)
        • * means zero-or-more spaces
        • $ is anchor for end of the line
      • / with below
        • \1 back-reference of above capture group
      • / end of substitution
      • q quit to avoid reading rest of the file unnecessarily
    • } close group command.
  • d delete any other lines before seen line number 2.

get into variable:

your_variableName=$(sed '2{ s/^classB = \(AB[^ ]*\) *$/\1/;q } ;d' your_fileName)
αғsнιη
  • 2,627
  • 2
  • 25
  • 38
3

Could you please try following, looks should be easy in awk. Considering you want to print 2nd line and print only digits in last field.

secondLine=$(awk 'FNR==2{sub(/[^0-9]*/,"",$NF);print $NF}' Input_file)
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2

You may try this awk:

awk -F ' *= *' '$1 ~ /B$/ { print $2 }' file
AB1234567
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thanks. Can you please use the variable `secondLine` and put this into another variable called `searchedtext` or something? – AdeleGoldberg Oct 29 '20 at 16:24
  • you can do `secondLine=$(awk -F ' *= *' '$1 ~ /B$/ { print $2 }' file)` to put output in a variable – anubhava Oct 29 '20 at 16:26
1

I'm not 100% sure this is what you're looking for, but if you know there's only a single element in the file that starts with AB, this will get it into a variable:

$ cat sample.txt 
classA = Something
classB = AB1234567
classC = Something more
classD = Something Else
  
$ x=$(perl -ne 'print if s/^.*\s+(AB\S+)\s*$/$1/' sample.txt)
 
$ echo "the variable is: $x"
the variable is: AB1234567

Explanation of the regex:

  • ^ beginning of line
  • .* anything
  • \s+ any number of spaces
  • (AB\S+) anything that starts with AB followed by non-spaces
  • \s*$ Zero or more spaces followed by the end of the line.
ShawnMilo
  • 5,896
  • 3
  • 19
  • 15