Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
Using awk to retrieve a specific set of strings from a file

textFileA consists of:

mango:oval:yellow:18pcs
apple:irregular:red:12pcs
orange:round:orange:4pcs

I wanted to do something like allowing user input for example user search "mango" I want the system to print out

Please enter the fruit name : mango >> this is user input

Desire Output

Fruit Shape : Oval
Fruit Color : Yellow
Fruit Quantity : 18pcs

So far this is what i done and it could only print out the entire line of strings, did i do something wrong here?

echo -n "Please enter the fruit name :"
read fruitName
awk '/'$fruitName'/ {print}'  textFileA

Current Output

mango:oval:yellow:18pcs
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

0

If you wish to format string by inserting certain values inside it you might find useful printf Statement, consider following example, let file.txt content be

mango:oval:yellow:18pcs
apple:irregular:red:12pcs
orange:round:orange:4pcs

then

awk 'BEGIN{FS=":"}/mango/{printf "Shape %s\nColor %s\nQuantity %s\n",$2,$3,$4}' file.txt

output

Shape oval
Color yellow
Quantity 18pcs

Explanation: I inform GNU AWK that field separator is : (read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR if you want to know more) then for line containg mango I printf string, %s are replace with values of columns: 2nd ($2) 3rd ($3) 4th ($4), \n denotes newline, so this printf does output multiline string. Note trailing newline, as by default printf does not include it at end.

(tested in gawk 4.2.1)

over 4 years ago · Santiago Trujillo Relatório

0

Using awk

$ awk -F: 'BEGIN {printf "Please enter fruit name: "; getline fruit < "-"} $1==fruit {print "Fruit Shape: " $2 "\nFruit Color: " $3 "\nFruit Quantity: " $4}' input_file
Please enter fruit name: mango
Fruit Shape: oval
Fruit Color: yellow
Fruit Quantity: 18pcs
$ cat file.awk
BEGIN {
    printf "Please enter fruit name: "; getline fruit < "-"
} $1==fruit {
    print "Fruit Shape: " $2 "\nFruit Color: " $3 "\nFruit Quantity: " $4
}
$ awk -F: -f file.awk input_file
Please enter fruit name: mango
Fruit Shape: oval
Fruit Color: yellow
Fruit Quantity: 18pcs
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda