1753599 Members
6381 Online
108796 Solutions
New Discussion юеВ

Cut column

 
SOLVED
Go to solution
Dennis Handly
Acclaimed Contributor

Re: Cut column

Please attach a file or explain better what you want to do. Specify the exact columns you want to extract and in your attachment provide the result.

>from column 10 to last 10 column

I'm not sure what "to last 10 column" means?
heaman1
Regular Advisor

Re: Cut column

thx dennis ,

sorry to confused you , I attached a file , could please provide advise ? if still not clear , please point it out , I will explain more detail .

thx
Dennis Handly
Acclaimed Contributor

Re: Cut column

>I attached a file, could please provide advise?
>that mean erase the character that from column 1 to 10 and from column 60 to 70

I assumed you wanted to just keep columns 11 to 60. And only wanted to remove the 10 columns from 61 to 70.

awk '
BEGIN {
getline
if ($0 ~ /aaaa/) {
str = substr($0, 11, 61-11)
# print str
sub("^ *", "", str) # strip leading spaces
# print str
sub(" *$", "", str) # strip trailing spaces
print str
} else {
print "Do nothing"
}
exit
} ' file
heaman1
Regular Advisor

Re: Cut column

it works for almost all the requirement ,

but what I hope to do is not to cut the column from column 60 to 70 , what I want is to cut the last ten column ( in the example , 60 -70 is last ten column , in real case , the length of the line is variable ) , could advise how to change it ? thx
Venkatesh BL
Honored Contributor

Re: Cut column

Looks like you are expecting others to do your job! :)

I would urge you to appreciate the folks who've helped you thus far, by assigning points. Refer to http://forums11.itrc.hp.com/service/forums/helptips.do?#28
Dennis Handly
Acclaimed Contributor

Re: Cut column

>what I want is to cut the last ten column

Why didn't you say exactly that. You said "to last 10 column", which (to) doesn't go together.

>(60 - 70 is last ten column

No your edge arithmetic is wrong, last 11 columns.

>could advise how to change it?

Trivial:
str = substr($0, 11, length($0)-2*10)
heaman1
Regular Advisor

Re: Cut column

thx reply ,

It run perfectly .
I still have one more question , the str is the result of the script , but it only output the result in the loop , can advise how to make the str result as a variable of the script ? like the below , I added echo $str at the end of it ( after exit ) , how to let it output the result of str ? thx

awk '
BEGIN {
getline
if ($0 ~ /aaaa/) {
str = substr($0, 11, 61-11)
# print str
sub("^ *", "", str) # strip leading spaces
# print str
sub(" *$", "", str) # strip trailing spaces
print str
} else {
print "Do nothing"
}
exit
} ' file

echo $str
Dennis Handly
Acclaimed Contributor

Re: Cut column

>can advise how to make the str result as a variable of the script?

It might be better to use Steven's sed solution.

>how to let it output the result of str?

You wrap it in $():
str=$(awk '
BEGIN {
getline
if ($0 ~ /aaaa/) {
str = substr($0, 11, length($0)-2*10)
sub("^ *", "", str) # strip leading spaces
sub(" *$", "", str) # strip trailing spaces
print str
} else {
print "Do nothing"
}
exit
} ' file)

If our answers were helpful, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33

.. assigned points to 0 of 56 responses
http://forums.itrc.hp.com/service/forums/pageList.do?userId=WW165205&listType=unassigned&forumId=1

If you don't assign points, you run the risk of being struck off.