Manipulating awk $variables using sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating awk $variables using sed?
# 1  
Old 04-04-2006
Manipulating awk $variables using sed?

I have been searching around the forums here trying to find a solution to my problem but not getting anywhere but closer to baldness.

I have a 20 column pipe "|" seperated text file. The 14th variable doesnt always exist, but will have the format of YYYYMM or YYYY if it does.

I need to take the 14th variable, and modify it to a YYYY-MM-DD format.

10000056|13615619|Something||||3533545|Something|Else|||||200405|78791|Disc|MUS000000||||

So, that 200405 needs to look like 2004-05-00, or 2004-00-00 if no month, 0000-00-00 if no value for $14 is available.

Any advice? Is it possible to apply that kind of variable sed to an awk variable?

awk -F\| '{print $1-$13 `echo $14|sedfoo` $15-$20}' is what I was dreaming of..

Last edited by r0sc0; 04-04-2006 at 05:44 PM..
# 2  
Old 04-04-2006
Code:
nawk -v OFS='|' -F'|' '{$14 = ($14 == "") ? "000-00-00" : substr($14,1,4) "-" substr($14,5) "-00";print}' myFile

# 3  
Old 04-04-2006
Im impressed and thoroughly grateful. Thanks! Works perfect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Updating variables using sed or awk

Hi, I have a file(testfile.txt) that contains list of variables as shown below. T $$FirstName=James $$LastName=Fox $$Dateofbirth=1980-02-04 ……and so on there are 50 different variables. I am writing a script(script1.sh) that will update the above three variable one by one with the values... (6 Replies)
Discussion started by: Saanvi1
6 Replies

2. UNIX for Dummies Questions & Answers

Use of Variables in a sed/awk script

Hi, After looking at the differents post on this forum, I am convinced that I will benefit from the experience of advanced Unix user on some script I have already done for an aeronautical study. Here is one of them : Step 1 : sed -e "s/??/00/g" Base_Awk.txt > Awk_Cut_00.txt4; sed... (11 Replies)
Discussion started by: Marc_Camoc
11 Replies

3. Shell Programming and Scripting

Manipulating variables in shell script

Hello, I know this should be simple but cant find a solution yet.I have the following in a sh script called "var" #!/bin/bash var1=0 And on another script called "main" I use a if construct: #!/bin/bash . var if then Do this else do that fi Now in "do this" part,I have to change... (8 Replies)
Discussion started by: vijai
8 Replies

4. Shell Programming and Scripting

Help with manipulating environmental variables in UNIX

I am wondering if there is away to increment a date in c shell. What I need to do is basic, but I lack the knowledge. I have they following environmental variable in my job scripts setenv YYYY `date '+%Y'` I then set YYYY to be part of my output dataset name: setenv dd_OUTPUTP... (1 Reply)
Discussion started by: jclanc8
1 Replies

5. Shell Programming and Scripting

Manipulating xyz file with awk-if-else or sed

Hi forum, I am really hoping somebody can please help me here. I have a dataset in xyz format, with longitude as x, latitude as y and data readings as z. eg. 0 90 -8 1 90 23 2 90 -4 etc etc etc What i am looking to do is format the data so that x and y are untouched, however in... (2 Replies)
Discussion started by: shlam16
2 Replies

6. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

7. Shell Programming and Scripting

Manipulating env variables for user apache?

So that they can be used in a cgi script? How best to do this? Thanks ---------- Post updated at 06:24 PM ---------- Previous update was at 02:38 AM ---------- Anyone that can help me with this? Basically I want to add an environment variable that will be visible to the cgi scripts when I... (0 Replies)
Discussion started by: stevenswj
0 Replies

8. Shell Programming and Scripting

Manipulating a header file using awk or sed

Hi Guys, Is there a simple way of doing the below. Available<spaces>Assigned<spaces>Maximum<spaces>Maximum<spaces>Page<spaces>Total <spaces>Used<spaces>Pct<spaces>Max. Pct<CR> Space<spaces>Capacity<spaces>Extension<spaces>Reduction<spaces>Size<spaces>... (8 Replies)
Discussion started by: eo29
8 Replies

9. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies

10. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question