LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2018, 12:16 PM   #1
d.mintis
LQ Newbie
 
Registered: Aug 2018
Posts: 4

Rep: Reputation: Disabled
open file 1 and write to 4th column a certain pattern


This is the format of file 1
20336 20336 20301
20337 20336 20301
20338 20336 20304
20339 20336 20304
20340 20336 20307
20341 20336 20307

I want to create a new file which will have the following format using awk in bash
20336 20336 20301 20302
20337 20336 20301 20303
20338 20336 20304 20305
20339 20336 20304 20306
20340 20336 20307 20308
20341 20336 20307 20309
 
Old 08-02-2018, 12:23 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,748

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Homework?
Show us what you've tried. We're happy to help.
 
Old 08-02-2018, 12:26 PM   #3
d.mintis
LQ Newbie
 
Registered: Aug 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
it is more practicing. I tried the following awk '{$(NF+1.5)=20302+1*int(NR-1)); print}'

So
1. I set a new field
2. I need to find the right calc so as to create the following numbering pattern -> 1 2 4 5 7 8 ....
 
Old 08-02-2018, 02:42 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,748

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by d.mintis View Post
it is more practicing. I tried the following awk '{$(NF+1.5)=20302+1*int(NR-1)); print}'

So
1. I set a new field
2. I need to find the right calc so as to create the following numbering pattern -> 1 2 4 5 7 8 ....
Can't help much with awk, but it looks like you're skipping every third number, yes? So only print if the number is not divisible by 3. Don't have a clue how to do that.

Have you read the awk man page or done a comprehensive web search?
 
Old 08-02-2018, 05:53 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,152

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
You can't have fractions of field numbers - $NF+1 will do the job. A subsequent print will include it as a field of $0.
How you scale it is your decision.
 
Old 08-02-2018, 08:33 PM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,382

Rep: Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761
Quote:
I need to find the right calc so as to create the following numbering pattern -> 1 2 4 5 7 8 ....
If this pattern is to continue 1 2 4 5 7 8 10 11 ...
perhaps something like
Code:
awk '{if (NR%2) i+=1; print $0,20300+NR+i,NR+i-1}'
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting the source file as per the code(1st substring) in 4th(AcctID) column bishnumnnit2006 Programming 40 02-27-2018 03:59 PM
How to read CSV data and compare the column values and then write them in new file VijayaRaghavanLakshman Linux - Newbie 9 01-26-2012 09:02 PM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] Read a number x from a file and write a character in the xth column of other file homexylo Programming 7 08-05-2011 05:28 AM
How do you write a magic file test pattern to match the end of a file? arj Linux - Software 3 02-10-2011 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration