Andrewziac
Andrewziac

Reputation: 155

Extracting specific text from a text file in UNIX

I'm having some problems with UNIX shell scripting, specifically file reading. What I would like the end product to be is for the script to take a text file as a command line argument and then extract certain parts to use in various operations. The text file would look like this:

ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

and it would continue like that with several other lines. Now what I have done so far to extract the number after the last colon is here in this code snippet:

case $1 in

   m)cat $2 | while read -r file; do
   #gets the numbers from 0 to 100
   current=grep [0-100]

The case statement is just because in the end the user will be able to run the program different ways. The main idea in the code segment however is to take the 2-digit number at the end of the line in the text file and store it in the current variable.

The rest of the operations really revolve around this idea, however, I'm not entirely sure how to extract the name in the middle.

Anyway, any help would be great! Just please keep in mind I am very new to this.

Upvotes: 0

Views: 2237

Answers (4)

user1011046
user1011046

Reputation: 204

AWk -F: '{print $NF}' file_name

Upvotes: 0

Kent
Kent

Reputation: 195039

there are many ways to extract name and score in your case. see example:

kent$  cat t
ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

#using awk 
kent$  awk -F: '{print "name="$2,", score="$3}' t                                         
name=John Smith , score=78
name=Jane Doe , score=80
name=Bob Johnson , score=91

#using cat
kent$  sed -r 's/[^:]*?:([^:]*):([0-9]*)$/name=\1, score=\2/g' t
name=John Smith, score=78
name=Jane Doe, score=80
name=Bob Johnson, score=91

#or just catch it directly with grep
kent$  grep -Po  "(?<=:)[^:]*(?=:)|(?<=:)\d+$" t
John Smith
78
Jane Doe
80
Bob Johnson
91

cut can do it as well.

Upvotes: 0

William Pursell
William Pursell

Reputation: 212218

Try this:

$ while IFS=: read a b c; do echo $c; done < input.txt

That will echo the third field of each line. Modify to suit your needs.

Upvotes: 1

David Wolever
David Wolever

Reputation: 154454

As frankc suggests, awk or cut would work well. You could also fiddle with IFS and (assuming Bash) arrays:

_old_ifs="$IFS"
IFS=":"
ID_NAME_GRADE=( $LINE )
IFS="$_old_ifs"

echo "Hello ${ID_NAME_GRADE[1]}, your grade is ${ID_NAME_GRADE[2]}"

Upvotes: 1

Related Questions