2

I did this in the past without problems, but I can't this time and I don't understand why.....

My original files is

1002       10214
1002       10220
1002       10222
1002       10248
1002       10256

I need to make a new file where the 2 columns above are merged and add a second column with value 1

Desired output should look like this

100210214    1
100210220    1
100210222    1
100210248    1
100210256    1

I tried the below awk commands to first print the 2 columns into 1 into a tmp file, then adding the extra column with "1"

cat input.txt |  awk '{ print ($1$2)}' > tmp1.txt

cat tmp1.txt | awk ' {print $0, (1) }' > output.txt

While the first command seems working ok, the second does not

tmp1.txt (OK)

100210214    
100210220    
100210222    
100210248    
100210256   

output.txt (not OK)

 10210214
 10210220
 10210222
 10210248
 10210256

The "1"comes in the front of the first column, not sure why, even replacing the first 2 characters. Is it because the original input file is different (may be "space" was used instead of tab)?

esse
  • 31
  • 3

2 Answers2

1

Could you please try following.

awk 'BEGIN{OFS="\t"} {sub(/\r$/,"");print $1 $2,"1"}'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

This happens when input file has Windows line endings (i.e. \r\n). You can fix it using this command:

dos2unix file

and then get the desired output with this one:

awk '{$1=$1$2;$2=1}1' file
oguz ismail
  • 1
  • 16
  • 47
  • 69