Get a Substring from a File Shell Script

How to extract a substring from file in shell script

Assuming your file is called test.txt, launch this command

cat test.txt | cut -f3 -d\|

Explanation:

The cut command filters a text cutting fields, delimited by a given separator

-f3 -->  the third parameter
-d\| ---> with | as delimiter

get string from lines of file in bash

$ cat data 
postgres 2609 21030 0 12:49 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 2758 21030 0 12:51 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 28811 21030 0 09:26 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32200 21030 0 11:40 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32252 21030 0 11:41 ? 00:00:00 postgres: postgres postgres [local] idle in transaction I need to extract second column from each line,
$ awk '{print $2}' data
2609
2758
28811
32200
32252

or you can squeeze multiple spaces into 1 using tr and then use cut like this:

$ tr -s ' ' < data | cut -d ' ' -f 2
2609
2758
28811
32200
32252

Edit:

$ tr -s ' ' < data | cut -d ' ' -f 2 | while read -r line || [[ -n "$line" ]]; do
> echo "$line" #put your custom processing logic here
> done
2609
2758
28811
32200
32252

Extract substring in Bash

Use cut:

echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2

More generic:

INPUT='someletters_12345_moreleters.ext'
SUBSTRING=$(echo $INPUT| cut -d'_' -f 2)
echo $SUBSTRING

How to find a substring from some text in a file and store it in a bash variable?

The following will work.

ver="$(cat config.txt | grep apache: | cut -d: -f3)"

grep apache: will find the line that has the text 'apache:' in it.

-d specifies what delimiters to use. In this case : is set as the delimiter.
-f is used to select the specific field (array index, starting at 1) of the resulting list obtained after delimiting by :

Thus, -f3 selects the 3rd occurence of the delimited list.

The version info is now captured in the variable $ver

Extract substrings from a file and store them in shell variables

Try this if you're using bash:

$ declare $(awk '{print $2"="$4}' file)
$ echo "$parent"
192.168.1.2

If the file contained white space in the values you want to init the variables with then you'd just have to set IFS to a newline before invoking declare, e.g. (simplified the input file to highlight the important part of white space on the right of the = signs):

$ cat file
parent=192.168.1.2 is first
child1=192.168.1.21 comes after it
child2=and then theres 192.154.1.2

$ IFS=$'\n'; declare $(awk -F'=' '{print $1"="$2}' file)
$ echo "$parent"
192.168.1.2 is first
$ echo "$child1"
192.168.1.21 comes after it

Get substring from file using sed

You should use is

sed -n 's/.*\".*\", \"\(.*\)\".*/\1/p' yourFile.txt

which means something (.*) followed by something in quotes (\".*\"), then a comma and a blank space (,), and then again something within quotes (\"\(.*\)\").

The brackets define the part that you later can reuse, i.e. the string within the second quotes. used it with \1.

I put -n front in order to answer the updated question, to get online the line that was manipulated.

In shell, how to extract substring from a complete file path

You can use bash parameter expansion:

baseFolder="/a/b/c/"
completeFilePath="/a/b/c/x/y/z.txt"

echo "${completeFilePath#$baseFilePath}"

Refer: http://wiki.bash-hackers.org/syntax/pe#substring_removal

Extract substring from a file shell script

This seems to work here.

tcpdump -Xvv -i wlan1 -n -nn proto \TCP -c 10 > capture.txt

sed -n 's/.*\.\(.*\): Flags.*/\1/p' capture.txt
443
35673
443
35071
80

How to extract substring from a text file in bash?

You want basename:

$ basename /tmp/accept/FLWS14UU.png
FLWS14UU.png


Related Topics



Leave a reply



Submit