awk - cut out and write new


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - cut out and write new
# 1  
Old 08-18-2012
awk - cut out and write new

Hi

i'm need help by the following problem.

i have csv-data like...
Code:
dd/mm/YYYY
...
01/01/20[COLOR="rgb(255, 0, 255)"]12[/COLOR],03:00,249.10,8.70,2,2,3,1,45240.0,[COLOR="rgb(255, 0, 255)"]223[/COLOR]
01/01/2012,03:10,249.60,9.10,2,2,3,1,50700.0,223
01/01/2012,03:20,247.70,8.30,2,2,3,1,55680.0,223
01/01/2012,03:30,250.00,9.00,2,2,3,1,61080.0,223
01/01/2012,03:40,248.90,9.10,2,2,3,1,66540.0,223
...

and i want to achieve a new file like ..
Code:
mm/dd/yy
OFS = \t
...
01/01/12             223
01/01/12             223
01/01/12             223
01/01/12             223
01/01/12             223
...

i try to solve it with a sript, mainly with awk
Code:
#!/bin/bash
f=file.dat
home=$HOME/directory/
cd $home
	if test -f "$f" 
	then
	Dd=${i:2:2} Dm=${i:4:2} Dy=${i:6:2}
	UZh=${i:8:2} UZm=${i:10:2}
	res=$(awk '{print $10}' OFS='\t' "$f")
	printf "%s/%s/%s-%s:%s\t%s\n" $Dm $Dd $Dy $UZh $UZm "$res"
	else
	DAT=$(date +%Y%m%d)
        printf "$DAT - Datei $f do not exist" >> "$home//Logfiles/Log_$(date +%Y%m%d).log"
    fi

Especially reading only the last two numbers of the year-variable.
i think also, my plan is to complicated - maybe you can help me to do it more easier and mor efficient.
thanks in advance!
IMPe
# 2  
Old 08-18-2012
Try this:
Code:
sed 's+^\([0-9][0-9]\)/\([0-9][0-9]\)/[0-9][0-9]\([0-9][0-9]\)+\2/\1/\3+' input.csv | awk -F, '{print $1,$NF}' OFS="\t"

This User Gave Thanks to mirni For This Post:
# 3  
Old 08-18-2012
Quote:
Originally Posted by mirni
Try this:
Code:
sed 's+^\([0-9][0-9]\)/\([0-9][0-9]\)/[0-9][0-9]\([0-9][0-9]\)+\2/\1/\3+' input.csv | awk -F, '{print $1,$NF}' OFS="\t"

Thanks!! is working fine. I only have to change the $NF to $10 in the awk-part, because there are some data-gaps.

Thanks!
IMPe
# 4  
Old 08-18-2012
You may try this:

Code:
cut -c 1-6,9- input.csv |cut -d, -f 1,10|tr ',' '\t'

-Rob.

Last edited by Rob Butler; 08-18-2012 at 11:21 PM.. Reason: Forgot the input file. Was just piping it in my test.
This User Gave Thanks to Rob Butler For This Post:
# 5  
Old 08-18-2012
Quote:
Originally Posted by mirni
Try this:
Code:
sed 's+^\([0-9][0-9]\)/\([0-9][0-9]\)/[0-9][0-9]\([0-9][0-9]\)+\2/\1/\3+' input.csv | awk -F, '{print $1,$NF}' OFS="\t"

A simpler approach:
Code:
awk -F, '{print substr($1,1,6) substr($1,9,2), $NF}' OFS='\t' input.csv

Regards,
Alister
# 6  
Old 08-19-2012
Quote:
Originally Posted by alister
A simpler approach:
Code:
awk -F, '{print substr($1,1,6) substr($1,9,2), $NF}' OFS='\t' input.csv

Regards,
Alister
At first I didn't notice his request to reverse mm and dd from the input file, so your approach is most likely best as cut can't reorder fields like awk can. But you'll need to change your code to:

Code:
awk -F, '{print substr($1,4,3) substr($1,1,3) substr($1,9,2), $NF}' OFS='\t' input.csv

-Rob.
This User Gave Thanks to Rob Butler For This Post:
# 7  
Old 08-19-2012
Quote:
Originally Posted by Rob Butler
At first I didn't notice his request to reverse mm and dd from the input file
I missed it completely, both in the sample data and in mirni's sed. Thanks for bringing it on home Smilie

Regards and welcome to the forum,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

write awk command into file using awk

hi, i want to write my nawk command into a file. i want to write: awk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gc_sw/again.mos'"'"' into gc.mos file. this is my code: awk '{print "awk 'NR==14 &&... (6 Replies)
Discussion started by: gc_sw
6 Replies

2. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

3. Shell Programming and Scripting

Cut 2 fields and write to a output file

Hi, I am writing a code where the file is a pipe delimited and I would need to extract the 2nd part of field2 if it is "ATTN", "C/O" or "%" and check to see if field9 is populated or not. If field9 is already populated then leave it as is but if field9 is not populated then take the 2nd part of... (3 Replies)
Discussion started by: msalam65
3 Replies

4. Shell Programming and Scripting

Want to use awk instead of cut

I want to use awk instead of cut command. Following is my code: line="slNumber=US78AJF11643, slName=USJFKAAUSYDAAUL80441032900095, dummy sl found? sqlca.sqlcode=0" sl_WORD=`echo $line| cut -f 1 -d','` sl=`echo $sl_WORD | cut -f 2 -d'='` echo "$sl" Please suggest me about the code. ... (5 Replies)
Discussion started by: rinku
5 Replies

5. Shell Programming and Scripting

HELP! using cut/awk

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. how would i do this using cut or awk methods is it possbile and having a output similar to this x, number of apperances = y, number of apperances = (2 Replies)
Discussion started by: deadleg
2 Replies

6. UNIX for Dummies Questions & Answers

Help please awk or cut

Hi I'm new to unix programming so struggling with something thats probably simple to many of you I have data files of the format : ID, date, value1, value2, blank on each line either value1 or value2 will be zero. I need my output file to contain ID, date, non-zero value The input... (3 Replies)
Discussion started by: thewench
3 Replies

7. Shell Programming and Scripting

Is awk vs cut which one is better

i was trying to work on program to look for users never log on sever.. using awk with awk is working last| awk '{print $1}' |sort -u > /tmp/users1$$ cat /etc/passwd | awk -F: '{print $1}' |sort -u > /tmp/users2$$ comm -13 /tmp/users$$ rm -f /tmp/users$$ with cut it is not working ... (3 Replies)
Discussion started by: macrules
3 Replies

8. Shell Programming and Scripting

awk or cut

select some fields from data file (source.csv) The data in file(source.csv) is like "x1,2",,"y",,"z" How to get the 1st, 2nd and 3rd field from the file. Using awk or cut? Note: "x1,2" is one field. thanks, (16 Replies)
Discussion started by: anypager
16 Replies

9. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies
Login or Register to Ask a Question