Deleting lines in .txt with nonspecific value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting lines in .txt with nonspecific value
# 1  
Old 08-15-2010
Deleting lines in .txt with nonspecific value

Hello, i am new to the forum and know nothing about programing, Linux or Unix Smilie hope somebody can help me out.

I have a .txt file that i need to delete certain lines from. After searching the forum i noticed that using "sed" was the way to go, so i installed gnuwin32 (i use windows xp Smilie) and started deleting lines with an specific value using this command:

Code:
sed /garbage/d inputfile > output

but now i would like to delete lines that fall into a certain category, for example lines with "more than 3 numbers" or "less than 4 characters".

This is a lot more complicated, or at least it seems that way to me, so if anyone knows how to do it i would appreciate the help, thanks.
# 2  
Old 08-15-2010
you can a write script for more safely but i can also try a something Smilie

"more than 3 numbers"
Code:
 
# sed '/[0-9]\{4,\}\|[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}/d' infile

"less than 4 characters"
Code:
# sed '/.*[A-Za-z]\{4,\}\|[A-Za-z]\{1,\}[^A-Za-z]\{1,\}[A-Za-z]\{1,\}[A-Za-z]\{1,\}[^A-Za-z]\{1,\}\|[A-Z]\{1\}[^A-Za-z]\{1\}[A-Z]\{3,\}|\[A-Z]\{2\}[^A-Za-z]\{1,\}[A-Z]{2,\}\|[A-Z]\{3\}[^A-Za-z]\{1,\}.*\|[A-Z]\{1,\}[^A-Za-z]\{1,\}[A-Z]\{1,\}[^A-Za-z]\{1,\}[A-Z]\{1,\}[^A-Za-z]\{1,\}[A-Z]\{1,\}\|[A-Za-z]\{3\}[^A-Za-z]\{1,\}[A-Za-z]\{1,\}\|[A-Z]\{1,\} [A-Z]\{1,\} [A-Z]\{1,\}\|[A-Z]\{1,\} [^A-Z]\{1,\} [A-Z]\{1,\} [^A-Z]\{1,\} [A-Z]\{1,\} [^A-Z]\{1,\}\|^[^A-Za-z][^A-Z]*$/!d;/\b[^A-Za-z]\{1\}[A-Z]\{1\}[^A-Za-z]\{1,2\}[A-Z]\{1,2\}\|\b[A-Z]\{3\}[^ A-Z]\{1,\}\|^[A-Z] [A-Z] [A-Z]$/d' infile

you maybe must use like this Smilie

Code:
### justdoit ###
# "find the line more than 3 numbers" or/and "less than 4 characters"
 
#!/bin/bash
 while read -r l
 do
   if [ $(echo "$l" | tr -d '[:alpha:]' | sed 's/ *//g' | wc -c) -gt 4 ] ; then
        `sed -i "/$l/d" infile`
   elif [ $(echo "$l" | tr -d '[:digit:]' | sed 's/ *//g' | wc -c) -lt 5 ] ; then
        `sed -i "/$l/d" infile`
   else
        echo "line has different condition->" `sed -n "/$l/p" infile`
   fi
 done < infile
 more a


regards
ygemici
# 3  
Old 08-15-2010
Thanks for your response! but i couldnt get it to work, i tried:

Code:
# sed /[0-9]\{4,\}\|[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}/d infile

But that didnt change anything, notice that i dont use single quotes cause i dont know how to write them in gnuwin, i press alt+39 and instead of getting " ' " i get something like this " ` " and i get "unknown command", could that be the reason?

And to make it "delete lines with more than 4 numbers" it would be something like these right?

Code:
# sed /[0-9]\{5,\}\|[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}/d infile

But since i am doing something wrong or the code is missing something it doesn't work.

Then regarding this:
Code:
### justdoit ###
# "find the line more than 3 numbers" or/and "less than 4 characters"
 
#!/bin/bash
 while read -r l
 do
   if [ $(echo "$l" | tr -d '[:alpha:]' | sed 's/ *//g' | wc -c) -gt 4 ] ; then
        `sed -i "/$l/d" infile`

   elif [ $(echo "$l" | tr -d '[:digit:]' | sed 's/ *//g' | wc -c) -lt 5 ] ; then
        `sed -i "/$l/d" infile`

   else
        echo "line has different condition->" `sed -n "/$l/p" infile`
   fi
 done < infile
 more a

I like this code, since this section of the code:

Code:
   if [ $(echo "$l" | tr -d '[:alpha:]' | sed 's/ *//g' | wc -c) -gt 4 ] ; then
        `sed -i "/$l/d" infile`

is a lot simpler than getting the same results on "sed" alone, but i dont have "bash" in my gnuwin32 files, i downloaded it, but can it be used from the gnuwin32 console at all? i couldn't even run the install from the bash folder.


Sorry but as i said, i dont know anything about this, so i can do little on my own.

Last edited by luis3141; 08-15-2010 at 08:56 PM..
# 4  
Old 11-22-2010
Hi, I was reading a lot of forums about editing *.txt files due to problem i have: I need to write something that will delete all lines that are different from:
"...
xxxx

1234 xxxx

5678 xxxx
..."
Where "xxxx" is whatever signs, numbers or letters, but ends with a new line...

i was trying to do it in ANSI C, but reading about it made me realize that it may not be the best solution... anybody can help?
# 5  
Old 11-22-2010
WYTIWYG what you type is what you get

Hello,

ygemici's
# sed '/[0-9]\{4,\}\|[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}/d' infile

Your's
# sed /[0-9]\{4,\}\|[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}.*[0-9]\{1\}/d infile

as you see, it's not the same !

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting Repeating lines from a txt file via script

Hi, I'm having trouble in achieving the following scenario. There is a txt file with thousands of lines and few lines are repeated, which needs to be removed using a script. File.txt 20140522121432,0,12,ram Loc=India From=ram@xxx.com, To=ravi@yyy.com,, 1 2 3 4 . . 30... (18 Replies)
Discussion started by: Gautham
18 Replies

2. Shell Programming and Scripting

Getting lines from .txt file

Hi I have a file with contents: NAMES John carrey williams How can I get all the names and store them in seperate variables(or arrays) please keep in mind that the no. of such names is not known.Three here is a bogus value ~thanks (4 Replies)
Discussion started by: leghorn
4 Replies

3. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

4. Shell Programming and Scripting

Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum :b::D: I recently tried something like this, I want to replace all those numberings/letters that are located between <string>file://localhost/var/mobile/Applications/ and /Documents/</string> numberings =---- replace with: first... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

6. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

7. UNIX for Dummies Questions & Answers

Deleting/Removing sentence from .txt

Hi, now i need to remove the entires i inserted into my .txt file. echo -n "Title: " read Title echo -n "Author: " read Author if grep -q "$Title: $Author" "BookDB.txt"; then sed '$Title: $Author' BookDB.txt echo "Book Title '$Title' removed successfully!" ... (6 Replies)
Discussion started by: santonio
6 Replies

8. Homework & Coursework Questions

Delete first both lines from .txt

Hi, I have to programming a program in shell script which combine a lot of .txt files. But in all of these files the program had to delete the first both lines. Because I don't know anything about shell script I need your help. Does anyone have a command or a hint, where I can look for? ... (1 Reply)
Discussion started by: mkrol
1 Replies

9. Shell Programming and Scripting

Delete first and second lines from .txt

Hi, I have to programming a program in shell script which combine a lot of .txt files. But in all of these files the program had to delete the first both lines. Because I don't know anything about shell script I need your help. Does anyone have a command or a hint, where I can look for? ... (1 Reply)
Discussion started by: mkrol
1 Replies

10. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies
Login or Register to Ask a Question