Using Awk to Get a Specific String in Line

Using awk to get a specific string in line

Here is one way:

ps -ef | awk -F"process.name" '{split($2,a," ");print FS a[1]}'
process.name=java1

find a string in a string using awk

If you only want to search the sixth column, use:

awk '$6 ~ /a/' file

If you want the whole line, any of these should work:

awk /a/ file

grep a file

sed '/^[^a]*$/d' file

How to extract only specific strings from each line of a file using awk?

With the current examples you can do it with grep like this:

<ext.txt grep -oE "(code is|code must be) '?[A-Z0-9]{11}'?" | 
tr -d "'" |
grep -o '[^ ]*$'

Output:

MGTCBEBEECL
MGTCBEBEE01
PARBFRPPXXX
CITIFRPPXXX
CIBCCATTXXX

How to print specific string from a sentence using awk

You might use regular expression as FS. Let file.txt content be

FQDN=joe.blogs.com.

then

awk 'BEGIN{FS="[=.]"}{print $2}' file.txt

output

joe

How to extract a specific line strings using awk

Assumptions:

  • the line(s) of interest are of the form <name>:<phone>:<rest_of_line>
  • matching will be based solely on the name (first) field
  • the full name (spelling and case) is known in advance otherwise we need to look at modifying the match logic to work on a) a substring and/or b) case sensitivity

Setup:

$ cat susan.txt
ignore this line
Susan Dalsass:(206) 654-6279:250:60:50
ignore this line

For this particular case there's really no need to use a (complicated?) regex when we can use some basic string matching capabilities of awk ...

Exact match on first field:

$ awk -F':' '$1=="Susan Dalsass" { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279

Match on leading part of the input line:

$ awk -F':' '/^Susan Dalsass/ { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279

Using a bash variable for an exact match on the name:

$ fullname="Susan Dalsass"
$ awk -v name="${fullname}" -F':' '$1==name { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279

Using a bash variable to do a partial match on the name:

$ partname="Susan"
$ awk -v name="${partname}" -F':' '$1~name { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279

Using a bash variable to do a partial, case-sensitive match on the name:

$ partname="saSS"
$ awk -v name="${partname}" -F':' 'tolower($1)~tolower(name) { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279

How to use awk to print lines where a field matches a specific string?

In awk:

awk '$2 == "LINUX" { print $0 }' test.txt

See awk by Example for a good intro to awk.

In sed:

sed -n -e '/^[0-9][0-9]* LINUX/p' test.txt

See sed by Example for a good intro to sed.

Using awk to retrieve a specific set of strings from a file

You may use it like this:

read -p "Please enter the fruit name: " fruitName
awk -F: -v fruitName="$fruitName" '
$1 == fruitName {
print "Fruit Shape :", $2
print "Fruit Color :", $3
print "Fruit Quantity :", $4
}' file

Output:

Please enter the fruit name: mango
Fruit Shape : oval
Fruit Color : yellow
Fruit Quantity : 18pcs

How to find and match an exact string in a column using AWK?

1st solution: Using split function of awk. With your shown samples, please try following awk code.

awk -F':' '
{
num=split($4,arr,",")
for(i=1;i<=num;i++){
if(arr[i]=="adm1"){
print
}
}
}
' Input_file

Explanation: Adding detailed explanation for above.

awk -F':' '               ##Starting awk program from here setting field separator as : here.
{
num=split($4,arr,",") ##Using split to split 4th field into array arr with delimiter of ,
for(i=1;i<=num;i++){ ##Running for loop till value of num(total elements of array arr).
if(arr[i]=="adm1"){ ##Checking condition if arr[i] value is equal to adm1 then do following.
print ##printing current line here.
}
}
}
' Input_file ##Mentioning Input_file name here.



2nd solution: Using regex and conditions in awk.

awk -F':' '$4~/^adm1,/ || $4~/,adm1,/ || $4~/,adm1$/' Input_file

OR if 4th field doesn't have comma at all then try following:

awk -F':' '$4~/^adm1,/ || $4~/,adm1,/ || $4~/,adm1$/ || $4=="adm1"' Input_file

Explanation: Making field separator as : and checking condition if 4th field is either equal to ^adm1,(starting adm1,) OR its equal to ,adm1, OR its equal to ,adm1$(ending with ,adm1) then print that line.

Use Awk to extract substring

You just want to set the field separator as . using the -F option and print the first field:

$ echo aaa0.bbb.ccc | awk -F'.' '{print $1}'
aaa0

Same thing but using cut:

$ echo aaa0.bbb.ccc | cut -d'.' -f1
aaa0

Or with sed:

$ echo aaa0.bbb.ccc | sed 's/[.].*//'
aaa0

Even grep:

$ echo aaa0.bbb.ccc | grep -o '^[^.]*'
aaa0


Related Topics



Leave a reply



Submit