How to change variable length field?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change variable length field?
# 1  
Old 06-03-2013
How to change variable length field?

Hello,

I have a file with a date field with various lengths. For example:

m/d/yyyy hh:mm or h:mm
mm/dd/yyyy hh:mm or h:mm

Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format.

Thanks in advance.

Jack
# 2  
Old 06-03-2013
Please post sample input and desired output.
# 3  
Old 06-03-2013
Quote:
Originally Posted by sonnyo916
Hello,

I have a file with a date field with various lengths. For example:

m/d/yyyy hh:mm or h:mm
mm/dd/yyyy hh:mm or h:mm

Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format.

Thanks in advance.

Jack
I presume you have a file with content like below

12/12/2012 22:30
4/2/1987 20:00

and the output that you desire is like below:
12/12/2012
4/2/1987

If this is the case you can simply user awk or cut whatever you are comfortable with and give the delimiter as [[SPACE]]
sample for cut
Code:
cut -d " " -f1

sample for awk
Code:
awk -F " "'{print$1}'

If i did not understand your issue let me know with a sample please
# 4  
Old 06-03-2013
Input is 7/10/2012 19:21 or 7/10/2012 1:01 or 7/1/2012 23:59 or 7/1/2012 1:01

Want it to be 7/10/2012

Fields may be 11/10/2012 01:01 but still need to be 11/10/2012.
# 5  
Old 06-03-2013
How about this?

awk '{print $1}' input

assuming that there is no space anywhere in the date-only part...
# 6  
Old 06-03-2013
Code:
awk '{gsub(/[^0-9/ ]|[0-9]+:[0-9]+/,x);$1=$1}1' file

# 7  
Old 06-03-2013
It could be done with sed (but I'm not going to bother).

With any shell that recognizes basic Bourne shell syntax:
Code:
while IFS="/ " read -r m d y extra
do      printf "%02d/%02d/%s\n" "$m" "$d" "$y"
done < file

does the job.

The same thing in awk would be:
Code:
awk -F '[/ ]' '{printf("%02d/%02d/%s\n", $1, $2, $3)}' file

If a file named file contains:
Code:
1/1/2013 1:23
1/10/2012 12:25
11/1/2011 1:02:03
12/30/2020 05:06:07

either of the above will produce:
Code:
01/01/2013
01/10/2012
11/01/2011
12/30/2020

If you choose the awk script and you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
=============
Oops, I hadn't noticed your 2nd posting where you said:
Quote:
Input is 7/10/2012 19:21 or 7/10/2012 1:01 or 7/1/2012 23:59 or 7/1/2012 1:01

Want it to be 7/10/2012
I have absolutely no idea how to write general code to determine that the day 1 should be printed as 10.

Should "12/9/2012" be also converted to "12/90/2012" instead of "12/09/2012"?

Last edited by Don Cragun; 06-03-2013 at 07:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

3. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

4. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

5. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

6. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

7. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

9. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question