Display all lines after a matching variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display all lines after a matching variable
# 1  
Old 02-02-2016
Display all lines after a matching variable

Hi,

I have a file called abc.txt with the following dates
Code:
2016-01-27
2016-01-28
2016-01-29
2016-01-30
2016-01-31
2016-02-01
2016-02-02
2016-02-03

I would like to print all lines below if 2016-01-31 is found, excluding that date.
I use this command -->
Code:
sed '1,/2016-01-31/d' abc.txt

If i declare the date in a variable and then try this, its not working. How do I include the variable in the sed command in Linux.
Moderator's Comments:
Mod Comment This is the 5th infraction you have received for not using CODE tags.

Please review the rules you agreed to when you joined this forum. And, watch the tutorial on using CODE tags in the private message you received a few minutes ago.

Continued refusal to correctly format your posts may result in you being banned from posting in this forum.

Last edited by Nagesh_1985; 02-02-2016 at 03:29 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-02-2016
Hello Nagesh_1985,

Please use code tags as per forum rules for commands/inputs/codes which you are using in your posts.
Could you please try following and let me know if this helps you.
Code:
awk '($0 ~ /2016-01-31/){A=1;next} A{print}'  Input_file

Output will be as follows.
Code:
2016-02-01
2016-02-02
2016-02-03

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-02-2016 at 04:29 AM..
# 3  
Old 02-02-2016
Try double quoting:
Code:
DT=2016-01-31
sed "1,/$DT/d" file
2016-02-01
2016-02-02
2016-02-03

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-02-2016
Quote:
Originally Posted by RavinderSingh13
Hello Nagesh_1985,

Please use code tags as per forum rules for commands/inputs/codes which you are using in your posts.
Could you please try following and let me know if this helps you.
Code:
awk '($0 ~ /2016-01-31/){A=1;next} A{print}'  Input_file

Output will be as follows.
Code:
2016-02-01
2016-02-02
2016-02-03

Thanks,
R. Singh
In short
Code:
awk 'A; /2016-01-31/{A=1}'  Input_file

All these solutions require the searched string to be present.
Here is one with a string comparison (that of course requires a sorted input)
Code:
awk '($0"" > "2016-01-31")'  Input_file

# 5  
Old 02-02-2016
Thanks RudiC

The command worked !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

2. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

3. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

4. UNIX for Dummies Questions & Answers

sed print certain lines matching variable

I have a very large results file, and a list of filters grep -wf filterlist.txt datafile.txt > outfile.txt The above line works but is very slow and I'm wondering how to make it faster. The items in my filterlist are only relevant to the first column. I don't care if any item in the... (1 Reply)
Discussion started by: sayb
1 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

Delete lines upto matching path variable

I have a script that if it crashes I want it to start again where it stopped. The way to identify the tasklist is using a find command that list all the files that need the process performed on them TASKLIST=`find /path/to/directory -iname *.dummy | sort` The way to identify the... (6 Replies)
Discussion started by: Bashingaway
6 Replies

8. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

9. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

10. Shell Programming and Scripting

Pattern matching in file and then display 10 lines above every time

hiii, i have to write a shell script like this---- i have a huge log file name abc.log .i have to search for a pattern name "pattern",it may occur 1000 times in the log file,every time it finds the pattern it should display the 10 lines above the pattern. I appericiate your help. (30 Replies)
Discussion started by: namishtiwari
30 Replies
Login or Register to Ask a Question