Shell script to solve query

Hi I have data in the below format in two columns in excel which i will copy to notepad.

test as rec1,	string
test as rec2,	byteint
test as rec3,	string
update date as test,	datetime
name as tes2	string

I need to add trim function on all the string columns and keep the remaining same. As below

o/p

trim(test) as rec1,	
test as rec2,	
trim(test) as rec3,	
update date as test,	
trim(name) as tes2	

There are 50000 columns this way and hence i am looking for something in script to do that.

Any help is appreciated.

Welcome to the forum.

Any attempts / ideas / thoughts fro your side?

I was thinking to do a search on the second field and if it contains string then add "trim( at the beginning of the first filed and ")" at the end before as. Not sure how I can do it though I know we can use sed for that.

How about

awk '$NF == "string" {$1 = "trim(" $1 ")"} {NF--; $1 = $1}  1' file

This has been tested under linux and FreeBSD; it may fail with other awk versions. Unfortunately you don't mention your OS, shell, tools' versions.

Thanks Rudic. Its only working for 1 row out of sample 4 rows i had.

My shell is bash and I am on Linux.

My result with your input data from post#1

trim(test) as rec1,
test as rec2,
trim(test) as rec3,
update date as test,
trim(name) as tes2

It's working for that. but when i change the i/p to below its bnot working

Tpc.CALCULATED_AMT      as      CALCULATED_AMT  ,date
Tpc.SERVICE_ID  as     SERVICE_ID       ,string
TRC.ITEM_NO     as      ITEM_NO  ,date
TPC.LAST_UPD_TIMESTAMP  as      LAST_UPD_TIMESTAMP      ,string

Of course not. It's a different structure.

Are you saying that commas are disappearing from the output? If not, what, exactly, is not working?

Did you notice that the commas are connected to the last field in your data sample in post #7 but they were connected to the next to the last field in your data sample in post #1? Did you notice that there is no comma on the last line of your sample input in post #1, but there is a comma on the last line of your sample input in post #7?

What output are you hoping to get from the sample data provided in post #7?

PS Please use CODE tags (not QUOTE tags) when displaying sample input, output, and code segments.

There are some spaces in my first field as well as my delimiter "," and the second field. So I am looking for o/p to be like the one I mentioned.

The trim is disappearing when there are spaces between field 1 and delimiter ","

Try

awk '$NF ~ "string" {$1 = "trim(" $1 ")"} {$NF = ","}  1' file
Tpc.CALCULATED_AMT as CALCULATED_AMT ,
trim(Tpc.SERVICE_ID) as SERVICE_ID ,
TRC.ITEM_NO as ITEM_NO ,
trim(TPC.LAST_UPD_TIMESTAMP) as LAST_UPD_TIMESTAMP ,

If that does not satisfy you need, please carefully create a reasonable, consistent specification including input and output samples and the logics connecting the two.