Help with cut


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with cut
# 1  
Old 11-09-2009
Help with cut

Hello,

I am trying to grep through some http log files and am having difficulty refining cut to my requirements.

I am cutting out everything but the column I want using "cut -f 5"

Which gives me just the lines I am after. I would like to refine this even more, to cut out everything before the characters XXXX and than cut all text after any five characters. so something like

cut (everything before "XXXX" and * (to the max of five characters) and cut everything after that.

Make sense?

Many Thanks
TTLGY
# 2  
Old 11-09-2009
I think you want sed.

If you know what XXXX is, use:

Code:
sed 's/.*XXXXX.*/XXXXX/

But I do not understand what you mean by "and * (to the max of five characters) and cut everything after that."

---------- Post updated at 08:27 PM ---------- Previous update was at 08:25 PM ----------

Oh, did you mean XXXX plus any five characters?

Code:
echo 123456XXXXX12345pppp | sed 's/.*\(XXXXX.....\).*/\1/'
XXXXX12345

Like that?

---------- Post updated at 08:58 PM ---------- Previous update was at 08:27 PM ----------

No, you said "up to 5". I always get confused by sed regexes so I go to Perl:

Code:
#!/usr/bin/perl
while (<>) {
 s/.*(XXXXX.{1,5}).*/$1/;
 print;
}

echo 123456XXXXXXX123456778 | ./t.pl
XXXXX12345

$ echo 123456XXXXXXX123 | ./t.pl
XXXXX123

Is that what you mean?

---------- Post updated at 09:11 PM ---------- Previous update was at 08:58 PM ----------

Ugly because of all the quoting, but with sed it's

Code:
sed 's/.*\(XXXXX.\{1,5\}\).*/\1/'


I hope with all that you have the idea of how this stuff all works.
# 3  
Old 11-09-2009
Yeah, so right now I am using

Code:
zcat /home/filename* | cut -f 3 | grep "XXXX" | less

Which outputs eg

sdasdasdaXXXXXsiynfSSSSSS
sdasdssXXXXXswednHHHHH
suenfghsddfsdfsdcvgwefrdsfbiXXXXXwugdnUUUUUUUUUUUUUUUUUU

All I want to output is

XXXXXsiynf
XXXXXswedn
XXXXXwugdn

I tried

Code:
zcat /home/filename* | cut -f 3 | sed 's/.*\(XXXXX.\{1,5\}\).*/\1/' | less

But that didn't return anything close to the match of XXXXX

I assume I am doing something wrong here?

Thanks for the help so far!
# 4  
Old 11-09-2009
show a sample of your input log file, what you want to get, and show your final output
# 5  
Old 11-09-2009
Here we go:

Sample:

hdyshdnfh 4425448447424 sdasdasdaXXXXXsiynfSSSSSS kfuhskdjfvbdfkjv fdlkghdflgdf
udfndfsvksdgv 54454581454 sdasdssXXXXXswednHHHHH hjdbfgsdyhbvdkfuhvfhdb fldkgjdflkg
dfdsfdvdfhdfgvd 64689686518684684 suenfghsddfsdfsdcvgwefrdsfbiXXXXXwugdnUUUUUUUUUUUUUUUUUU hfgudhfhyfbfggf fdkghjdfjg

I want to output

XXXXXsiynf
XXXXXswedn
XXXXXwugdn

I just want to extract all of the matches of XXXXX (and the next five letters)
# 6  
Old 11-09-2009
Code:
$ awk '{match($0,"XXXXX"); print substr($0,RSTART,10)}' file
XXXXXsiynf
XXXXXswedn
XXXXXwugdn

# 7  
Old 11-09-2009
Works for me:

$
Code:
 cat t
sdasdasdaXXXXXsiynfSSSSSS
sdasdssXXXXXswednHHHHH
suenfghsddfsdfsdcvgwefrdsfbiXXXXXwugdnUUUUUUUUUUUUUUUUUU
$ cat t| sed 's/.*\(XXXXX.\{1,5\}\).*/\1/'
XXXXXsiynf
XXXXXswedn
XXXXXwugdn

 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question