LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2010, 03:45 AM   #31
redhatuser1
Member
 
Registered: Sep 2009
Posts: 55

Original Poster
Rep: Reputation: 0
awk + if help -


Following on from a different help - many thanks to redhatuser and rm

At the minute I have the code:
Code:
awk 'BEGIN{FS=",";OFS=",";count=1}
{
if ($0 ~ /^C/){
	gsub(" ","",$5)
	if($5=="B"){
		$2=sprintf("BCS%06u",count); count++
	}
}
print
}' < step8.csv > newfile.csv
echo "Editing extract, step 10, selecting BACs records and amending Number" >> $LOGFILE
echo "" >> $LOGFILE
It incrementally increases a number in one of the fields. I need to record this number and start from the last position in the next script. Redhat user gave some good advice and a bit works - unfortunately I can't get it to use the variable in the awk statement -any advice?

I have this code which dumps the variables to file:
Code:
awk -v count=$count --dump-variables=some_temp_file \
Then this code to read the code for the previous count
Code:
count=$(awk '/^count:/{gsub("[[:punct:]]","",$3);print $3}' filename
Can someone tell me how where I should have these statements? Is a For statement better than IF in this scenario? How should I modify the awk?

Last edited by redhatuser1; 10-28-2010 at 04:12 AM.
 
Old 10-28-2010, 04:27 AM   #32
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
How about:
Code:
#!/usr/bin/awk -f

BEGIN{ OFS=FS=","
       count=1

    cmd = "test -e some_temp_file"
    if(!system(cmd)){
        getline count < "some_temp_file"
        close("some_temp_file")
    }
}

/^C/{
    gsub(" ","",$5)
    if($5=="B")
	$2=sprintf("BACS%06u",count++)
}

1

END{
    print "Editing extract, step 10, selecting BACs records and amending Cheque Number" >> "filename"
    print "" >> "filename"

    print --count > "some_temp_file"
}
Not tested but I think you get the idea
 
Old 10-28-2010, 05:41 AM   #33
redhatuser1
Member
 
Registered: Sep 2009
Posts: 55

Original Poster
Rep: Reputation: 0
can you explain how it works - i am one of those real noobs.. Love Linux but you gotta learn by doing..
 
Old 10-28-2010, 05:59 AM   #34
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well most of it is what you had before, but instead of dumping all the args I simply printed the final count (-1 as it would have incremented) to your temp file
and then test if the file exists next time it starts then it reads in the count and away you go

What I have given you is a stand alone script for awk, ie you can run this without a bash script wrapper.
The other difference is your LOGFILE variable obviously doesn't exist as this is not bash so you would have to supply the name. You could however use the same variable name
and just set it in the BEGIN.

Oh and the last thing is, '1' in awk means do default action which is to print.

Let me know if you need more clarification?
 
Old 10-28-2010, 06:39 AM   #35
redhatuser1
Member
 
Registered: Sep 2009
Posts: 55

Original Poster
Rep: Reputation: 0
Considering I require it to be intergrated into a larger script, is what I am doing a suitable solution?

If there is a different way to search the record for all lines starting with "C" then edit the 2nd column depending on the content of the 5th column. If the 5th column is "B" then edit the 2nd column with an incremental number prefixed with B(or whatever). Finally retain this number and start there the next time the script is executed.
 
Old 10-28-2010, 07:55 AM   #36
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
In that case I might go all bash:
Code:
#!/bin/bash

exec 6<&0 7>&1 <step8.csv >newfile.csv 8>>$LOGFILE

c=1
zeroes="000000"

[[ -e some_temp_file ]] && c=$(< some_temp_file)

while read -r line
do
    arr=(${line//,/ })

    if (( ${#arr[*]} > 4 )) && [[ ${arr[0]} == C && ${arr[4]} == B ]]
    then
        b_val="B${zeroes:0:$((${#zeroes} - ${#c}))}$((c++))"
        line=${line/${arr[1]}/$b_val}
    fi

    echo "$line"
done

echo "Editing extract, step 10, selecting BACs records and amending Number" >&8
echo "" >&8

exec <&6 >&7 6<&- 7>&- 8>&-

echo $c > some_temp_file

Last edited by grail; 10-28-2010 at 07:57 AM.
 
Old 10-28-2010, 08:03 AM   #37
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
grail,

if it may help, have a look over here for insight:

http://www.linuxquestions.org/questi...result-840521/

redhatuser,

I have requested that your new thread here, be merged with your ongoing thread from yesterday. As this is the exact same issue, and is still ongoing; please do not create a new thread (in the future) for the same subject for which you already have a thread going. This is considered double-posting.

Best regards, and continued good luck with this.
 
  


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
[SOLVED] sed help - replace line feed with different character bradvan Programming 7 04-23-2012 12:31 AM
How to replace a character with the output of some commands using sed? mamun2015 Linux - Newbie 18 03-16-2010 11:50 AM
Replace 2nd to last Character with SED elproducto Programming 5 03-31-2009 01:41 PM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 02:15 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 07:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration