LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 01:29 PM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,809

Rep: Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069

Well I think it is a shame for you to give up being so close But I hope you get to the solution you are looking for even if it is with bash

Still happy to answer anymore questions should you have them (although at nearly 3am I am off to bed )
 
Old 04-21-2011, 01:33 PM   #17
masavini
Member
 
Registered: Jun 2008
Posts: 269

Original Poster
Rep: Reputation: 6
ok, i gave up with that one... too complicate...
but i just got my first awk script!!
your efforts to teach me something were not wasted...

Code:
#!/bin/bash

cd /tmp/

echo -e "dv2000_g_52_b;5;50.01;\ndv2000_g_52_b;5;49.99;" > prrrr.txt

awk -v "file=prrrr.txt" '

BEGIN	{
		RS="[\r\n]+"
		FS=";"
		OFS=";"
		
		have = split("", field)
		while ((getline line < file) > 0) {
			FS=";"
			have = split(line, field)
			
			FS="_"
			have = split(field[1], subfield)
			
			if (subfield[2] == "g" && field[3] < 50) {
				subfield[2] = "r"
				field[1] = subfield[1] "_" subfield[2] "_" subfield[3] "_" subfield[4]
				print field[1] ";" field[2] ";" field[3] ";"
			}
				else
				print line
		}
	}'
a small question: is there a way to let this part a bit "lighter"?

Code:
				field[1] = subfield[1] "_" subfield[2] "_" subfield[3] "_" subfield[4]
				print field[1] ";" field[2] ";" field[3] ";"
 
Old 04-22-2011, 01:39 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,809

Rep: Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069
Well I am not sure if this is to be part of something bigger, but if not then you do not need the 'file' variable anymore as there is only one
being used so just pass it in. Also, all the splitting is not really needed, again assuming above, but also that split allows you to enter
an alternate value to split on apart from the FS value:
Code:
split(string, array [, fieldsep])
So you can see an optional fieldsep is able to be used.

Now it will depend on whether or not the letter you search for, ie. 'g' in your example, is elsewhere on the line, but the following simplified code works:
Code:
awk -F";" '{split($0,f,"_");if(f[2] == "g" && $3 < 50)sub(f[2],"r")}1' prrrr.txt
 
Old 04-23-2011, 06:01 AM   #19
masavini
Member
 
Registered: Jun 2008
Posts: 269

Original Poster
Rep: Reputation: 6
just wonderful...
and the most amazing thing is i can understand your code!
many thanks, once again...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
merge columns from multiple files vijay_babu1981 Linux - Newbie 21 06-24-2014 07:59 AM
[SOLVED] Comparing files in bash script brainlesseinstein Linux - Newbie 9 09-02-2010 01:41 PM
how to merge files in different folder using bash script kkpal Programming 5 04-21-2008 03:35 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 11:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration