Visit Our UNIX and Linux User Community


Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File
# 1  
Old 02-10-2015
Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All

It's me again with another huge txt files. Smilie

What I have:
- I have 33 huge txt files in a folder.
- I have thousands of line in this txt file which contain many the letter "x" in them.
- Some of them have more than one "x" character in the line.

What I want to achieve:
- Grab all the lines that has not more than one "x". In other words, I just want to have lines that only have one "x" character in it.

It doesn't just have to be using grep cause this isn't school project or challenge.

I've had used many grep command but still my knowledge is not good enough yet. I believe I also have to put "wc" command in this. [I don't know]
Last time I used wc, it was a successful failure. I tried to "mv" all that txt file names that has only one word into another directory & I screwed up big time Smilie So I worry "wc" might not work in this situation just like it doesn't work to search "directory" [Maybe it does, I just don't know]

So here is my slightest clue for this: [it doesn't work]

Code:
find .location -name "*.txt" -exec grep -l 'x' {1} + > Output.txt

# 2  
Old 02-10-2015
Something along the following might help

No more than one x:
Code:
grep -v "x.*x" .location/*.txt

Exactly one x:
Code:
grep x .location/*.txt|grep -v "x.*x"

This User Gave Thanks to Walter Misar For This Post:
# 3  
Old 02-10-2015
One x:
Code:
grep '^[^x]*x[^x]*$'


Last edited by MadeInGermany; 02-10-2015 at 03:42 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 02-10-2015
Wow! I can't believe I could get reply so fast.

I used:
Code:
grep -v "x.*x" .location/*.txt > output.txt

It worked perfectly! Thank you so much guys! Smilie Smilie

Last edited by Franklin52; 02-10-2015 at 08:45 AM.. Reason: Please use code tags
# 5  
Old 02-10-2015
Quote:
Originally Posted by Nexeu
Wow! I can't believe I could get reply so fast.

I used:

grep -v "x.*x" .location/*.txt > output.txt

It worked perfectly! Thank you so much guys! Smilie Smilie
You say this is working perfectly, but this will add lines from your input files that do not contain any x characters as well as those that contain one x character. Your initial requirements were conflicted as to whether or not lines with no x characters should be copied.

So, do you want lines with no x or not?
# 6  
Old 02-10-2015
Yes, I had noticed there are line without "x" is also included for the first command. At least that removed lines with double "x" or more.

Code:
grep -v "x.*x" .location/*.txt

And for that 2nd, it removed anything but lines with Only one "x".

Code:
grep x .path/*.txt | grep -v "x.*x" > output.txt

I kinda chose first option as I noticed some lines without "x" seems to be what's important. Smilie

But I was wondering if there might be a command with such functionality though:

- It will grep 33 .txt files and find lines without repeated "x" and it will write it into individual 33 .txt output files in a different directory

I have the clue something like this:

Code:
for file in .path/*.txt; do grep -v "x.*x" "${file%.*}.txt" -nobs -width 999 "$file" ; done

BUT, it will do the execution in the same directory. [In other words, I am screwed as the output has same extension and file name Smilie ] Thank god I didn't do that Smilie
# 7  
Old 02-10-2015
You mean something like:
Code:
cd .path;for file in *.txt;do grep 'x' "$file"|grep -v 'x.*x' > /output/directory/"$file";done

This User Gave Thanks to Don Cragun For This Post:
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #34
Difficulty: Medium
When a computer is first powered on, the special type of absolute loader is known as the pre-boot loader.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Grep all lines with the string "TNS-" but skip those with "TNS-12514"

Platform: Oracle Linux 6.3 From a log file, I want to grep all lines with the pattern "TNS-" but I want to skip those with the pattern "TNS-12514" . How can I do this ? (3 Replies)
Discussion started by: John K
3 Replies

3. Shell Programming and Scripting

Filter file by length, looking only at lines that don't begin with ">"

I have a file that stores data in pairs of lines, following this format: line 1: header (preceded by ">") line 2: sequence Example.txt: >seq1 name GATTGATGTTTGAGTTTTGGTTTTT >seq2 name TTTTCTTC I want to filter out the sequences and corresponding headers for all sequences that are less... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

4. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

5. Shell Programming and Scripting

Adding a "|" character for a text file

Dear Sir, I am having text file with no delimiter like below RAGAV S S 12358 SALES EXECUTIVE 25000 RAJU R B 64253 SALES EXECUTIVE 28000 RUKMAN S 32588 SALES EXECUTIVE 40000 NARGUND S S 12356 ... (3 Replies)
Discussion started by: suryanarayana
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. Shell Programming and Scripting

Appending character "0" in vi editor for all lines between 1 and 40.

I have to append character "0" for lines between 1 and 40 in a file. I tried the following code. :s/^0,1,40/g Input: Output: (2 Replies)
Discussion started by: pinnacle
2 Replies

9. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

Featured Tech Videos