Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Adding one to a specific column in tab-seperated...
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
freke
l33t
l33t


Joined: 23 Jan 2003
Posts: 977
Location: Somewhere in Denmark

PostPosted: Mon Dec 05, 2016 7:16 pm    Post subject: [SOLVED] Adding one to a specific column in tab-seperated... Reply with quote

Hi,

I have a tab-seperated file with values like this:
Code:
"RUTE"  "100 Afkast og Kiosk"   6       "64300" "Nysted bilen"  "Olga"  "Afksast aviser "       "Almindelig"    28                                                                                     
"RUTE"  "100 Afkast og Kiosk"   7       "64400" "Nordfalster bilen"     "Torben Ejlersen"       "afkast aviser "        "Almindelig"    28                                                             
"RUTE"  "100 Afkast og Kiosk"   21      "67200" "Sydvestlolland"        "John (Jette Clausen)"  "Afkast aviser "        "Almindelig"    33                                                             
"RUTE"  "100 Afkast og Kiosk"   22      "64800" "Nordlolland bilen"     "Ronni Sommer"  "afkast aviser "        "Almindelig"    28      "TILGANG"       "100"   "64800" "5253111"       "Erik Schultz"
"RUTE"  "100 Afkast og Kiosk"   32      "67000" "Sydlolland bilen"      "Jakob Muttalip Fener"  "Afkast aviser "        "Almindelig"    25                                                             
"RUTE"  "100 Afkast og Kiosk"   40      "64500" "Midtfalster bilen"     "Mads FT Hansen"        "Afkast aviser "        "Almindelig"    49      "AFGANG"        "100"   "64500" "5145027"
"RUTE"  "100 Afkast og Kiosk"   41      "64700" "Østfalster bilen"      "Jan Lise"      "Afkast aviser "        "Almindelig"    53                                                     
"RUTE"  "100 Afkast og Kiosk"   42      "64600" "Guldborg bilen"        "Michael Jensen"        "Afkast aviser "        "Almindelig"    22

ie. the field I want one added to is field 9 (ie. in the first line 28 should be 29 etc...)
Could that easily be done with a script?


Last edited by freke on Tue Dec 06, 2016 9:56 am; edited 1 time in total
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 30915
Location: here

PostPosted: Mon Dec 05, 2016 7:54 pm    Post subject: Reply with quote

Try with this
Code:
awk '
    BEGIN {
        FS = OFS = "\t"
    }

    {
        $8 = $8+1
        print $0
    }
' tab-separated-file.txt

_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3345
Location: Rasi, Finland

PostPosted: Mon Dec 05, 2016 9:15 pm    Post subject: Reply with quote

fedeliallalinea wrote:
Try with this
Code:
awk '
    BEGIN {
        FS = OFS = "\t"
    }

    {
        $8 = $8+1
        print $0
    }
' tab-separated-file.txt
I think awk counts from 1 so shouldn't all $8's be $9?
_________________
..: Zucca :..
Gentoo IRC channels reside on Libera.Chat.
--
Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Mon Dec 05, 2016 10:02 pm    Post subject: Reply with quote

freke ...

Code:
% awk -v FS="\t" -v OFS="\t" '{$9 = ($9 + 1) ; print}' input.txt
"RUTE"  "100 Afkast og Kiosk"   6       "64300" "Nysted bilen"  "Olga"  "Afksast aviser "       "Almindelig"      29
"RUTE"  "100 Afkast og Kiosk"   7       "64400" "Nordfalster bilen"     "Torben Ejlersen"       "afkast aviser "  "Almindelig"    29
[...]

best ... khay
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 30915
Location: here

PostPosted: Tue Dec 06, 2016 6:25 am    Post subject: Reply with quote

Zucca wrote:
I think awk counts from 1 so shouldn't all $8's be $9?

Yep :D .

@khayyam: thanks I didn't know the option -v
_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
freke
l33t
l33t


Joined: 23 Jan 2003
Posts: 977
Location: Somewhere in Denmark

PostPosted: Tue Dec 06, 2016 7:41 am    Post subject: Reply with quote

Thanks A LOT!

This saves me manually handling the file :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum