edit field using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting edit field using sed or awk
# 22  
Old 03-07-2011
Quote:
Originally Posted by yinyuemi
Code:
awk '$2=($2>10)?$2+0:"0"$2+0' file

thanks bro ...

---------- Post updated at 09:54 PM ---------- Previous update was at 09:36 PM ----------

Quote:
Originally Posted by yinyuemi
Code:
awk '$2=($2>10)?$2+0:"0"$2+0' file

what this code means ($2>10)? and $2+0: especially the ":"
thanks
# 23  
Old 03-08-2011
Code:
()?: is a short form of if condition code, here,
$2=($2>10)?$2+0:"0"$2+0 can be explained as:
{if ($2>10) {$2=$2} else {$2="0"$2+0}}

This User Gave Thanks to yinyuemi For This Post:
# 24  
Old 03-08-2011
TRY

awk '{print $1 " " substr($2,5,2)}' File_Name
This User Gave Thanks to suresh.boddepu For This Post:
# 25  
Old 03-08-2011
Code:
awk '$2=sprintf("%02d",$2)' file

This User Gave Thanks to Scrutinizer For This Post:
# 26  
Old 03-08-2011
Quote:
Originally Posted by zulabc
thanks bro ...

---------- Post updated at 09:54 PM ---------- Previous update was at 09:36 PM ----------



what this code means ($2>10)? and $2+0: especially the ":"
thanks
may be this is stupid question..is it any different between $2+0 and $2 + 0
# 27  
Old 03-08-2011
Hi, there is no difference.

---------- Post updated at 07:31 ---------- Previous update was at 07:29 ----------

Quote:
Originally Posted by suresh.boddepu
awk '{print $1 " " substr($2,5,2)}' File_Name
This will cut >2 digit numbers
# 28  
Old 03-08-2011
Quote:
Originally Posted by Scrutinizer
Hi, there is no difference.

---------- Post updated at 07:31 ---------- Previous update was at 07:29 ----------


This will cut >2 digit numbers
this code $2+0 to remove 0s.. can u tell me where to find this things to read/ refer..i thought the "+" symbol is to add something..

ur help are very much appreciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

2. Shell Programming and Scripting

Field overwrite using sed or awk

Is there a way to overwrite a specific field (i.e. line 2 field 3 without getting its contents). For example I would like to simply have a compatible Solaris 10 command line that replaces line 2 field 3 with contents of a variable. I would like to use SED or AWK if possible, but other suggestions... (1 Reply)
Discussion started by: thibodc
1 Replies

3. Shell Programming and Scripting

Inline edit using sed / awk

Hi, I have file with all the lines as following format <namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding" name="ENV_CONFIG_PATH" nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH" stringToBind="test"/> I want to replace (all the lines) value of... (8 Replies)
Discussion started by: shuklaa02
8 Replies

4. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

5. Shell Programming and Scripting

Conditional edit for a field using sed

Hi I want to repalce a field in a txt file on solaris with say 100 records and each record having a total of 10 fields separated by a ~ . based on the following condition the record should be edited or else the record should be written as it is to a if the seventh field is 'XX' and if... (2 Replies)
Discussion started by: acharania2011
2 Replies

6. Linux

awk/sed for splitting a field into two

I have a tab delimitted dataset with 4 fields. I like to split the second field into two, and have 5 fields. I like to remove the "-" sign when I get a new fiel. would you help? It is like: 1223 100-5 rr dd I need it like: 1223 100 5 rr dd (2 Replies)
Discussion started by: sire
2 Replies

7. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

8. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

9. UNIX for Dummies Questions & Answers

Adding a new field using sed or awk?

I have a bcp file that contains 10 fields. These fields are separated by a tab. How can I add my name as a new field in the 8th position for every record? I've been playing w/ sed and awk but can't seem to figure this out. (3 Replies)
Discussion started by: sasabune
3 Replies

10. Shell Programming and Scripting

sed / awk - inplace or inline edit

I need to remove the '&' from a file. In each line of the file, the fields are separated by ^K. I only want to remove '&' if it exists in field number 9. (example of field 9: abc&xyz) I need to do an in place/in line edit. So far I have accomplished the following: awk -F '^K' '{print... (6 Replies)
Discussion started by: hemangjani
6 Replies
Login or Register to Ask a Question