Delete all lines that start with a bigger number of a specific one.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all lines that start with a bigger number of a specific one.
# 1  
Old 11-16-2010
Tools Delete all lines that start with a bigger number of a specific one.

E.g. the file is like this:
Quote:
1
1
3
5
[thank]
3
1
{you}
8
All
8
9
I want to delete all lines that begin with a number larger than 2, ignoring the lines that doesn't begin with a number!
PS:'2' is actually a variable that can have a lot of valuesSmiliei bet you got it
# 2  
Old 11-16-2010
Depending on whether line starting with 2 should be retained :
Code:
sed 's/^0*//' yourfile | grep -vE '^[3-9]|^[12][0-9]'

or filtered out :
Code:
sed 's/^0*//' yourfile | grep -vE '^[2-9]|^1[0-9]'


Last edited by ctsgnb; 11-16-2010 at 03:45 PM..
# 3  
Old 11-16-2010
Code:
 
#/bin/ksh
echo "#/bin/ksh" > temp.ksh
echo "sed -e 's/^[$1-9].*//' -e '/^$/ d' file_name > temp" >> temp.ksh
echo "mv temp file_name" >> temp.ksh
chmod +x temp.ksh
./temp.ksh
rm -f temp.ksh

A better way (Ignore above pls):

Code:
 
#/bin/ksh
sed "/^[$1-9]/d" file_name > temp
mv temp file_name

This will work if lines have single digits (0-9)

Last edited by anurag.singh; 11-16-2010 at 03:36 PM..
# 4  
Old 11-16-2010
That won't work if the number is larger than one digit. For example "19", "1000", etc.

Try:

Code:
awk  '{ if (($0 ~ /^[^0-9]/) || ($1 <= 2)) {print $0}}' infile

# 5  
Old 11-16-2010
Code:
awk -v max=3  '!($1 ~ "^[0-9][0-9]*$" && $1>max)' yourfile

# 6  
Old 11-16-2010
Code:
# del=2;sed "/^[$del-9]/d" datafile

---------- Post updated at 09:24 PM ---------- Previous update was at 09:15 PM ----------

for like numbers >10
Code:
# del=2;sed "/^[1-9][0-9][0-9]*\|^[$del-9]/d" datafile

# 7  
Old 11-16-2010
Quote:
Originally Posted by verdepollo
That won't work if the number is larger than one digit. For example "19", "1000", etc.

Try:

Code:
awk  '{ if (($0 ~ /^[^0-9]/) || ($1 <= 2)) {print $0}}' infile

True, i fixed my code ... so it also handle case where line start with :

0000003456
0012
0013
0014
0015
001
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

3. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

4. UNIX for Dummies Questions & Answers

How to delete all columns that start with a specific value

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the columns that start with NA such that: File before modification aa bb cc NA100 dd aa b1 c2 NA101 de File after modification aa bb cc dd aa b1 c2 de How would I... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

A way to delete specific lines

Hi all, I hope an expert tells me that there is a way to get ride of the certain lines in my files which have no specific string on them but fixed length. It's the original pattern:... (9 Replies)
Discussion started by: GoldenFire
9 Replies

6. Shell Programming and Scripting

How to delete all lines before a specific word?

Let's say we have a file containing: alllllsadfsdasdf qwdDDDaassss ccxxcxc#2222 dssSSSSddDDDD D1Sqn2NYOHgTI Hello Alex ssS@3 Ok, and let's say we want to delete all words from D1Sqn2NYOHgTI and back, this means to delete the words (and the lines of them) : alllllsadfsdasdf... (2 Replies)
Discussion started by: hakermania
2 Replies

7. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

8. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

9. Shell Programming and Scripting

need to delete lines that start with letters

Hi, I need to remove all lines from a file that do not start with numbers For instance, if the first three characters on any line are not numbers, delete those lines I've tried to do it with awk and it's not working, any ideas ? Thanks (5 Replies)
Discussion started by: sfisk
5 Replies

10. UNIX for Advanced & Expert Users

How to delete specific lines at the same time

Dear All I have a pattern which look like this: 2 20080312_10:55:35.800 Spain-Telefonica ISC 9 IAM 927535957 34670505334 f 275 COT b 700 ACM b 6577 CPG b 10726 ANM b 202195 REL f 202307 RLC :COMMA: NCI=15,FCI=2101,CPC=0A,TMR=00,USI,OFI=00: :COMMB: BCI=0214,OBI=01,ACT: :RELCAUSE:10: This... (1 Reply)
Discussion started by: zanetti321
1 Replies
Login or Register to Ask a Question