Home > Mobile >  Finding all ocurrences from determinate word and exctracting the next word in bash
Finding all ocurrences from determinate word and exctracting the next word in bash

Time:12-02

I have a .txt file where the word 'picture:' is found multiple times in the file. How can I extract all words after the 'pictures:' word and save in a text file

I tried the follow code,but doesn't work:

cat users_sl.txt |awk -F: '/^login:"/{print $2}' cookies.txt

user_sl.txt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Quis picture lobortis scelerisque fermentum dui faucibus in ornare quam. Est ullamcorper eget nulla facilisi etiam dignissim diam quis. Quis viverra nibh cras pulvinar mattis nunc sed. Turpis massa sed elementum picture tempus egestas. Condimentum vitae sapien pellentesque habitant. Et molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Tincidunt lobortis feugiat vivamus at augue eget arcu picture dictum varius. Donec massa sapien faucibus et molestie ac feugiat sed. Tincidunt eget nullam non nisi est. Ornare arcu dui vivamus arcu. Mattis enim ut tellus elementum sagittis vitae et leo duis

picturelist.txt:

lobortis
dictum
tempus

CodePudding user response:

Well, I'm assuming you actually just have picture instead of **picture:**, and that you may need to deal with line breaks, so...

$ cat sl.txt 
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Quis picture lobortis scelerisque fermentum dui faucibus in ornare quam.
Est ullamcorper eget nulla facilisi etiam dignissim diam quis.
Quis viverra nibh cras pulvinar mattis nunc sed.
Turpis massa sed elementum picture tempus egestas.
Condimentum vitae sapien pellentesque habitant.
Et molestie ac feugiat sed lectus vestibulum mattis ullamcorper.
Tincidunt lobortis feugiat vivamus at augue eget arcu picture
dictum varius. Donec massa sapien faucibus et molestie ac feugiat sed.
Tincidunt eget nullam non nisi est.
Ornare arcu dui vivamus arcu.
Mattis enim ut tellus elementum sagittis vitae et leo duis

$ cat sl.txt | tr '\n' ' ' | grep -o 'picture [^ ]*' | cut -d' ' -f2
lobortis
tempus
dictum

CodePudding user response:

With :

#!/bin/bash

arr=( $(<user_sl.txt) )
for ((i=0; i<${#arr[@]}; i  )); do
    if [[ ${arr[i]} == picture ]]; then
        printf '%s\n' "${arr[i 1]}"
    fi
done | tee picturelist.txt

Output

lobortis
tempus
dictum

CodePudding user response:

With :

$ perl -nE 'say for /\bpicture\b\s (\w )\b/g' user_sl.txt | tee picturelist.txt
lobortis
tempus
dictum

CodePudding user response:

With :

$ awk '{
    for (i=1; i<=NF; i  ) {
        if ($i == "picture") print $(i 1)
    }
}' user_sl.txt | tee picturelist.txt

lobortis
tempus
dictum

CodePudding user response:

The code you provided seems to be incorrect. The awk command should be used to search for the word 'picture:' in the file and print the word that comes after it. Here is an example of how you could do that:

awk '/picture:/{getline; print}' users_sl.txt > output.txt

This command will search for the pattern 'picture:' in the file users_sl.txt, then get the next line and print it to a file called output.txt.

Here is a breakdown of the command:

  • awk: This is the command to run the awk program.
  • /picture:/: This is the pattern that awk will search for in the input file. In this case, we are searching for the word 'picture:'.
  • getline: This is an awk function that gets the next line from the input file.
  • print: This is an awk function that prints the current line to the output file.

CodePudding user response:

cat textfile                    | \
    grep -o 'picture:\*\*[^ ]*' | \
    sed 's/.*\*\(.*\)/\1/g'       \
    ;

CodePudding user response:

The code you provided is not working because there are some syntax errors in it. The correct syntax to extract all words after the 'pictures:' word and save it in a text file is:

cat users_sl.txt | awk -F "picture:" '{print $2}' > picturelist.txt

The above command will extract all words after 'picture:' in the input file and save it in a file named 'picturelist.txt'.

Here's how the command works:

  1. cat users_sl.txt - This prints the content of the file 'users_sl.txt' to the standard output.

  2. awk -F "picture:" '{print $2}' - This is the core part of the command. It uses the awk command to extract the words after 'picture:'. The -F option is used to specify the delimiter (in this case, it is 'picture:') and $2 is used to extract the second field (which is the words after 'picture:').

  3. > picturelist.txt - This redirects the output of the awk command to a file named 'picturelist.txt'.

I hope this helps. Let me know if you have any questions.

  • Related