Re: Cut column

 
SOLVED
Go to solution
heaman1
Regular Advisor

Cut column

I have a file , there are many lines , the content is as below .

p.s. the "------" is space

aaaaaa ------------------ testing1 ----------------- bbbb
aaaaa -----------------testing2 ----------bbbba
aaaaaaaaa ---------------testing3 -------------------------------------------- bbbd
aaaa -----------------testing4--bbbb
"
"

I would like to have a script to cut the content in the file to get the result , my desired result is as below

testing1
testing2
testing3
testing4
"
"

the condition to cut the content is
1. cut the content from the column 10 of each line
2. each line must be ended with a word that begins with bbb , so cut the content until bbb
3. erase all space ( -------- ) , then get the below result

testing1
testing2
testing3
testing4
"
"


Could advise the script ? thx in advance.
17 REPLIES 17
john123
Trusted Contributor

Re: Cut column

try awk '{print $2}' filename
heaman1
Regular Advisor

Re: Cut column

Sorry , I have an adjustment , what I want now is only do the first line of the file ( instead of cut the content of each line ) , so the result should be "testing1" .


Thx
Venkatesh BL
Honored Contributor

Re: Cut column

awk '{print $2}' |head -1
heaman1
Regular Advisor

Re: Cut column

thx reply ,

awk '{print $2}' |head -1

the above script is simple , but in my case , the words "testing1" is only example , in real case , it is a string that have space in it ( not just a word ) , so if I want to cut the culumn from column 2 to second last 2 column , what can i do ? thx
Steven Schweda
Honored Contributor
Solution

Re: Cut column

> the condition [...] is [...]

> Sorry , I have an adjustment, [...]

> [...] in real case [...]

Come back when you know what you want?

I'm not sure that the problem is well
specified, but something like the following
might be close to what you seek.

bash$ echo 'aaaaaa test ing 1 bbbb' | \
> sed -e 's/^.......... *//' -e 's/ *bbb[^ ]*$//'
test ing 1

The idea was to remove the first ten
characters ("^..........") plus any string
of spaces (" *"), and then to remove the
spaces leading up to "bbb" (" *bbb") and any
non-space characters between it and the end
of the line ("[^ ]*$").

(As I always say, "If you can't so it with
'sed', it's not worth doing.")

If you want only one line, "man head".
heaman1
Regular Advisor

Re: Cut column

Sorry , please ignore the my pervious message first , I think cut column 2 ( awk '{print $2}' |head -1 ) is not work for my case .

I would like to carify my requirement as below .

the file content
==========
aaaaaa ------------------ testing1 ----------------- bbbb


step 1
====
check if the line contain the word "test" , if not , then output the result "Do nothing" .

if have , then do step 2 .

step 2
====
cut the column from column 10 to last 10 column

so the result is

--------------- testing1 ------------

step 3
====
erase all space that in front of and in rear of the string "testing1" .

so the result is

testing
Steven Schweda
Honored Contributor

Re: Cut column

> Sorry , please ignore [...]

Will do. You're not paying enough to get
me to chase your ever-changing requirements.
I've provided a "sed" example which does
much of what you want (even now). If you
can't find someone to write your script for
you, perhaps you should try to study that
example, and adapt it to your (latest) needs.
heaman1
Regular Advisor

Re: Cut column

thx reply ,

the my last message would be my final requirement ( sorry to change requirement as I get something confused ) , I will change it again.

I tried sed -e 's/^.......... *//' -e 's/ *bbb[^ ]*$//'

the result is
--------------- testing1 -----------------

if I want to cut column as the my message , could advise what can i do ? thx
Steven Schweda
Honored Contributor

Re: Cut column

> I tried sed -e 's/^.......... *//' -e 's/ *bbb[^ ]*$//'
>
> the result is
> --------------- testing1 -----------------

I don't see how it could be. As I showed
before, I don't get those spaces.

Now, if they were tabs and not spaces, that
could change things. Of course, I can't see
what's really in your file, and there's no
reason to believe that you know what's in
there, either, and my psychic powers are too
weak to tell me, so I don't see what more I
can do.