Match and variable length string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match and variable length string
# 1  
Old 12-01-2011
Match and variable length string

Hello all,

source file looks like this:

Code:
cat toto.txt
NAME)
   VAR1=tata
   OPT4=toto
   USER=blabla
   TZ=/usr/share/zoneinfo/Hongkong
   OTHEROPT=something
   ;;
NAME2)
   VAR1=tata
   OPT4=toto
   USER=blabla
   TZ=/usr/share/zoneinfo/Hongkong
   SOMETHING=else
   ONEMORE=whynot
   ;;

I'm trying to get the name and timezone (TZ) info:

Code:
awk 'BEGIN { RS = ";;" } /TZ\=/  {print substr($1,1,length($1)-1) ":" substr($0,(match($0,/TZ.*/)),31)}' /etc/configtoto

The above code works BUT it wouldn't if there was a different value in TZ (right now its the same in all NAME but it might change)

I'm trying to find out a different way to get the TZ value. That 31 bothers me.

Thanks.
# 2  
Old 12-01-2011
Are you going to be filtering on specific record (NAME2) values? As in, why are you setting RS?

Otherwise, you could just do:
Code:
awk -F= '/TZ/ {print $2}' toto.txt

This might work with RS:
Code:
 awk 'BEGIN { RS=";;"; FS="[)= ]" } /TZ/ { for (i=1;i<NF;i++) { if ($i ~ /TZ$/) { print $(i+1) }}}' toto.txt

# 3  
Old 12-01-2011
Another try

Code:
awk -F= '/)/ { name=$0;sub(/.$/,"",name) } /TZ=/ { print name, $2}' /etc/configtoto

This User Gave Thanks to clx For This Post:
# 4  
Old 12-01-2011
I'm setting RS because there is other text, config, etc in that file not only the part i have included. Setting RS clears me out of those.

The output of course should be like this:

Code:
NAME:TZ=/usr/share/zoneinfo/hongkong
NAME2:TZ=/usr/share/zoneinfo/hongkong
NAME3:TZ=/usr/share/zoneinfo/hongkong
NAME4:TZ=/usr/share/zoneinfo/hongkong

Main problem would be to have a TZ value of lets say /usr/share/zoneinfo/GMT since the length is hardcoded.
# 5  
Old 12-01-2011
Better if you show the complete part ( at least just format, actual values changed)
# 6  
Old 12-01-2011
Try:
Code:
awk 'BEGIN { RS=";;"; FS="[)= ]" } /TZ/ { printf ("%s:", $1); for (i=2;i<=NF;i++) { if ($i ~ /^TZ$/) { printf ("TZ=%s\n", $(i+1)); }}}' toto.txt


Last edited by CarloM; 12-01-2011 at 11:14 AM..
This User Gave Thanks to CarloM For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. 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

3. Shell Programming and Scripting

Sed:- Supported variable replacement after string match?

Hi All, I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed. sed -e "s/URL./& http:\\localhost:7223/g" But when am trying to pass the variable it is failing. I tried multiple... (9 Replies)
Discussion started by: sharsour
9 Replies

4. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

8. Shell Programming and Scripting

Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with sed -e 's/args=\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'but that wasn't too difficult due to the value being 16 digits. The bank account... (7 Replies)
Discussion started by: mk4mzid
7 Replies

9. 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

10. Shell Programming and Scripting

Finding out the length of a string held within a variable

:confused: Does anyone know which command I can use to find out the length of a string held within a variable? (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question