Bash Command to Get Nth Line in a String?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Command to Get Nth Line in a String?
# 1  
Old 08-24-2015
Bash Command to Get Nth Line in a String?

hi

i need to get the 3rd line in a string.

actually, not sure if it's a string, list, array, or something else. I'm using grep to retrieve all the numbers in a string-- in the console, it displays as multiple lines:
Code:
$ echo "Sink 0: reference = 0: 153% 1: 45%, real = 0: 62%" | grep -o [0-9]* 
0
0
153
1
45
0
62

now i want to retrieve just the 3rd number in the list (153 in this case). How?

full disclosure, my source string is actually the output of another command, but wanted to keep the question simple.

btw, If there's a simpler way to retrieve the 3rd number in the original string, plz share. But note that there are embedded line-breaks in my actual source string (with which the above grep method has no problem).

thx!

(also asked on linuxforums.org)

Last edited by johnywhy; 08-24-2015 at 10:07 AM..
# 2  
Old 08-24-2015
Try
Code:
echo "Sink 0: reference = 0: 153% 1: 45%, real = 0: 62%" | sed -r 's/[^0-9]+/\n/g;s/^([^\n]*\n){3}//g;s/\n([^\n]*\n){4}$//'
153

# 3  
Old 08-24-2015
Hi, under bash:
Code:
$ readarray -s2 BOB < <(echo "Sink 0: reference = 0: 153% 1: 45%, real = 0: 62%" | grep -o "[0-9]*")
$ echo $BOB
153

with "-s2" for line number 3
This User Gave Thanks to disedorgue For This Post:
# 4  
Old 08-24-2015
Or
Code:
$ echo "Sink 0: reference = 0: 153% 1: 45%, real = 0: 62%" | awk -F ': ' '{print $3+0}'
153

or, shell only:
Code:
var="Sink 0: reference = 0: 153% 1: 45%, real = 0: 62%"
var2=${var#*: *: }
echo "${var2%%%*}"



--
Note: With sed, -r, [^\n] and \n in the replacement part are GNU extensions.
Note: readarray is bash 4 or higher...

Last edited by Scrutinizer; 08-24-2015 at 11:53 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 08-24-2015
Code:
 pacmd dump-volumes | awk -F': *' '/^Sink 0/{print 0+$3}'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete after nth occurence of string in each line

Hello, Environment: I am under Ubuntu 18.04 bionic. I have an sql file consisting of 10K lines. Objective: What I am trying to attain is to remove everything coming after 2nd tab in each line. While searching for the answer, I found two answers and both gave expected result just for the first... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

On the command line using bash, how do you split a string by column?

Input: MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e MD5(secret2.txt)= asd123qwlkjgre5ug8je7hlt488dkr0p I want the results to look like these, respectively: MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p Basically, keeping... (11 Replies)
Discussion started by: teiji
11 Replies

8. UNIX for Advanced & Expert Users

Command to display nth line before the string is matched.

All, Is there any way out to display the nth line before the string is matched ??? Eg : If i have a file which has the following contents and if i want to get the 3rd line before the string is matched a b c d e f if i give the input as f and lines before the match as 3 then it should... (5 Replies)
Discussion started by: helper
5 Replies

9. Shell Programming and Scripting

Replacing a string in nth line

Hello All, How to replace a string in nth line of a file using sed or awk. For Ex: test.txt Line 1 : TEST1 TEST2 TEST3 Line 2 : TEST1 TEST2 TEST3 TEST4 Line 3 : TEST1 TEST2 TEST3 TEST5 Line 4 : TEST1 TEST2 TEST3 TEST6 Line 5 : TEST1 TEST2 TEST3 TEST7 i want to go to 4th line of a... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question