For Loop Field editing - without using "awk"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop Field editing - without using "awk"
# 1  
Old 03-16-2012
For Loop Field editing - without using "awk"

Hi,

I'm using Linux and bash shell. I have a file (F1.txt) with contents like
Code:
 
Table1 Column1 123abc
Table1 Column2 xyz
Table2 Column1 543

Now, I would like to get the output as
Code:
UPDATE Table1 SET Column1='123abc';
UPDATE Table1 SET Column2='xyz';
UPDATE Table2 SET Column1='543';

It can be easily using "awk" as
Code:
#!/bin/sh
awk '
                {printf "UPDATE %s SET %s='%s';\n",$1,$2,$3;}
' F1.txt > s1.log

Due to a complex requirement, I would like to do it only in a FOR loop as given below. I do quite a lot of business processing inside a FOR loop,
which gets extended more and more. It will be boring, if I start mentioning all the details, in-depth.

I know "awk" is quite powerful, but how to do Field Editing without awk, using the example given below.
Code:
for rowdata in `cat F1.txt`
do
 ......statements
 ......statements
 echo  "UPDATE $rowdata (extract 1st Field) SET $rowdata (extract 2nd Field) = $rowdata (extract 3rd field);"
 
 ......statements
 ......statements
 
done >> s1.log

Thanks a lot.

Moderator's Comments:
Mod Comment Please use code tags!
for your code and data!

Last edited by vbe; 03-16-2012 at 05:15 PM.. Reason: code tags
# 2  
Old 03-16-2012
Using a 'for' loop to do this is a useless use of cat and useless use of backticks, precisely because of the reason you've discovered, among others -- it splits where it pleases, not on lines.

The shell's read builtin is much better suited. It can even split for you.

Code:
while read A B C
do
        echo "UPDATE $A SET $B='$C';
done < F1.txt

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-17-2012
Many thanks for providing the alternative! It does work well, good Smilie

Now, how do I process the delimited file other than Tab or Space, for example a CSV file using the same while looop, not using "awk", it does support.

Code:
Table1,Column1,123abc
Table1,Column2,xyz
Table2,Column1,543

Thanks a lot.

---------- Post updated at 02:01 AM ---------- Previous update was at 01:47 AM ----------

Hi,

I have tried the below code and it worked, please tell me if there is any other alternative method available, thank you.

Code:
 
#!/bin/sh
OLDIFS="$IFS"
IFS=,
while read f1 f2
do
        echo "update $f1 set $f2=123;"
done < t2.log > s2.log
IFS="$OLDIFS"

# 4  
Old 03-19-2012
Sorry for not getting back to this sooner.

You have found the right method for using delimiters with read. You can handle it a little more gracefully by doing

while IFS="," read ... so you don't need to permanently change the value of IFS, but that's just a minor improvement, it does fundamentally the same thing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk separate field by letter and ";"

i have a 2 fields in my DB ID25333,1429291340lNormPUC-AP_MEX_UFM-GOL_44;PUC-AP_VEX_UFM-ROL_55;PUCAP_MEX_UFM-DOJ_49; ID55555,1429291340lNormPUC-AP_PPP_UFM-HOL_44;PUC-AF_GEX_UJM-SOL_45;PUCAP_MEX_UFM-DOJ_59;and i need separate like this ID25333,PUC-AP_MEX_UFM-GOL_44; ... (5 Replies)
Discussion started by: Axl_north
5 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

[SOLVED] How to set field separator in awk when FS is "||" ?

Dear Experts, How to use field separator in awk , when the field separation is needed as "||" Example: (file1 ) aa || bb || cc || 1234 xx || yy || zz || 1123 Q: If I want to print $3 and $4 only , with using the field separator "||" How to write awk code: (6 Replies)
Discussion started by: rveri
6 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk: Force "escaped" representation of a field

Is there any way to get the escaped version of a field in awk? For those that don't understand the question, here is a clarifying example. Lets say a field $1 gives me the string "(dumb'" (I've changed the delimiter to be something other than whitespace). If i use that value in a command in awk... (2 Replies)
Discussion started by: IvanMalison
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question