I have this awk script. I have two different methods to remove the first column. However defining method = 1 in the BEGIN produces different results than when defining method = 1 later on instead. Any idea what it might be doing differently???
---------- Post updated at 08:36 AM ---------- Previous update was at 08:34 AM ----------
Input file looks something like this:
---------- Post updated at 08:39 AM ---------- Previous update was at 08:36 AM ----------
Having method = 1 in the body of the script gives the file below.
Notice how I get things replicated, once still having the first column and the next is without the first column.
I just need to print the line without the first column only
I agree with danmero, go with the logic that is a bit more straight forward. Yes, you need the header/trailer setup, and to establish your xi/xf variables, but the simple statement offered should do the trick. You could write it like this to make it a bit less 'magical'
I prefer to use a null string ("") instead of assuming that a variable (x in the example) isn't set. I also think it is easier to understand if $1 and FS are separated by a space. Including a print statement inside of the block is necessary, unlike the other example, as you want to print only when the condition that $1 is less than zf; placing the 1 after the block (impled {print}) would print all lines removing the first column only from the lines where the condition was true.
The reason for the duplicated lines is how/where you coded the assignment of method. Awk programmes consist of a basic syntax that is:
When you coded 'method = 1' with no action awk assumes a default action of print, so what you really had was:
which results in method being assigned 1 and evaluating to true, so the default action was taken. Thus the input line was printed before you drop the first column.
If you need to assign a variable like this, then do it in the BEGIN block.
Hope this helps to explain why you were seeing the duplicated lines in the output.
But the OP's question was about a small part of a larger awk script, not about command line suggestions.
@OP: I tried both your ways of removing the first column and they gave identical results, so why do you need two?. I would replace them with danmero's solution, but I agree with agama that relying on unitialized variables is better avoided in non-oneliner script, so I would also use "" instead of x in this case..
---------- Post updated at 09:11 ---------- Previous update was at 08:48 ----------
If you want to set the variable method lower in the script, you need to do it like this:
Then you will get identical results..
Last edited by Scrutinizer; 09-24-2010 at 04:43 AM..
Example:
I have files in below format
file 1:
zxc,133,joe@example.com
cst,222,xyz@example1.com
File 2 Contains:
hxd
hcd
jws
zxc
cst
File 1 has 50000 lines and file 2 has around 30000 lines :
Expected Output has to be :
hxd
hcd
jws (5 Replies)
Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2
Scenario 1
i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Hi
I have a text file (Input.txt) with two column entries separated by tab as given below:
aaa str1
bbb str2
cccccc str3
dddd str4
eee str3
ssss str2
sdf str3
hhh str1
fff str2
ccc str3
.....
.....
..... (1 Reply)
Hi,
I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns.
Below is the sample data file, I want to remove the comma(,) only from 5th column.
$ cat file1
ABC | DEF, HIJ|... (6 Replies)
SOLVED, thank you!
Edit2:
Good news everyone,
I managed to get it down to a "simple" problem, but I still have some syntax issues.
Here is the code which troubles me:
awk 'BEGIN{x2=0;x1=0;crit=0;}
$1 < 1000000 {x2=$4; diffx=x2-x1; x1=x2;
diffx > 3.6 ? {crit=1} : {crit=0};... (2 Replies)
Hi,
I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column.
clientname,day-of-month,max-users
-----------------------------------
client1,20120610,5
client2,20120610,2
client3,20120610,7... (3 Replies)
I have huge txt file having millions of trade data.
For e.g
Trade.txt (first 8 lines in the file is header info)
COB_DATE,TRADE_ID,SOURCE_SYSTEM_TRADE_ID,TRADE_GROUP_ID,
TRADE_TYPE,DEALER_NAME,EXTERNAL_COUNTERPARTY_ID,
EXTERNAL_COUNTERPARTY_NAME,DB_COUNTERPARTY_ID,... (6 Replies)