1

Here is my query :

/path/newdir/newtext.csv 

newtext.csv looks like below :

Record 1

line 1
line 2
Sample Number: 123456789 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)

Record 2

line 1
line 2
Sample Number: 363214563 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)

Record 3

line 1
line 2
Sample Number: 987654321 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)

Assume there are such 100 records in a newtext.csv So, now i need the parameters of the entered i/p string, which is something below

Example Input Search String :

123456789

Example Output :

Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z

This is what exactly i need. Can you please help me ?

1
  • @ Siva Charan : Thank u very much for putting up in a correct manner. Commented Jun 4, 2012 at 7:13

2 Answers 2

1

For plain input* string,

grep -F "InputString" -A27 inputFile.csv | sed -n '1p;19p;$p'

For pattern(Extended-regex)* string,

grep -E "InputPattern" -A27 inputFile.csv | sed -n '1p;19p;$p'

Script:

user$ cat script.sh

#!/bin/bash 
grep -F "$1" -A27 inputFile.csv | sed -n '1p;19p;$p'

user$ chmod +x script.sh
user$ ./script.sh "inputString"

Edit:

Non-line number based solution,

#!/bin/bash 
grep -F "$1" -A27 inputFile.csv |sed -n "/$1/p;/^Time\s[^:]*:/p"

* The input must be unique to the file.

Sign up to request clarification or add additional context in comments.

14 Comments

@ Prince John Wesley : Thank U very much, it's absolutely working fine.
Yeah, it ws mismatch with the line number. Bt ur command is absolutely perfect. Thank U very much. What does the symbol -F denote here ? Is the symbol '|' used to concat the two sommands ?
remove -F if you want to treat your input as regex pattern. -F helps to treat the input string as plain text.
Is the symbol '|' used to concat the two commands grep & sed ?
Thank U very much Prince John Wesley. It's really helped me alot. Keep ur support. I'm very new to this forum. Any suggestions to improve are most welcome.
|
0

Try This

csv.txt (Input File)

Sample Number: 123456789 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)
line 1
line 2
Sample Number: 363214563 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)
line 1
line 2
Sample Number: 987654321 (line no. 3)
|
|
|
|
|
Time In: 2012-05-29T10:21:06Z (line no. 21)
|
|
|
Time Out: 2012-05-29T13:07:46Z (line no. 30)

csv.sh (Code)

echo "Enter your search string:"
read name
grep -A 10 "$name" csv.txt | grep -v "|" | awk -F "(" '{print $1}'

Output

Enter your search string: 123456789 

Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z

13 Comments

@ Chronicles : That doesn't work according to our requirement. What exacltly cat csv.txt | grep -A 10 "$name" | grep -v "|" | awk -F "(" '{print $1}'does ?
@James grep -A 10 will get the details "after" 10 lines of match of the required query like "1234567" . Then grep -v will remove all the lines containing '|' . Then , at last the awk command will filter and print the required data.
Hi Chronicles, Thank u for ur input. What do u mean the lines containging '|' here ?
@ Chronicles : Is it possible to get the output in the form as below: IMSI number is : IMSI : 123456789 Connected Time is : Time In :2012-05-29T10:21:06Z Disconnected Time is : Time Out : 2012-05-29T13:07:46Z
@JackieJames: | is to pipe the output of one command to input of other
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.