Shell script to solve query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to solve query
# 1  
Old 01-20-2018
Shell script to solve query

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



Code:
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

Code:
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.

Last edited by Don Cragun; 01-21-2018 at 08:36 AM.. Reason: Change QUOTE tags to CODE tags.
# 2  
Old 01-20-2018
Welcome to the forum.

Any attempts / ideas / thoughts fro your side?
# 3  
Old 01-20-2018
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.
# 4  
Old 01-20-2018
How about
Code:
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.

Last edited by RudiC; 01-20-2018 at 05:07 PM..
# 5  
Old 01-21-2018
Thanks Rudic. Its only working for 1 row out of sample 4 rows i had.

My shell is bash and I am on Linux.
# 6  
Old 01-21-2018
My result with your input data from post#1
Code:
trim(test) as rec1,
test as rec2,
trim(test) as rec3,
update date as test,
trim(name) as tes2

# 7  
Old 01-21-2018
It's working for that. but when i change the i/p to below its bnot working

Code:
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


Last edited by Don Cragun; 01-21-2018 at 08:36 AM.. Reason: Change QUOTE tags to CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Solve a query please

Please solve the below: Use a pipeline and command substitution to set the length of a line in emp.lst to a variable. (1 Reply)
Discussion started by: ravisingh
1 Replies

2. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

3. Shell Programming and Scripting

shell script query

Hi Admins, I was trying to list the failed logins as part of my daily checklist. Here is my script for i in `who -s /etc/security/failedlogins|tail -100|grep "$dt"|awk '{print $1" "$6}'` do a=`echo $i|wc -l` if then echo $i else echo "There are no failed logins" fi done but... (3 Replies)
Discussion started by: newaix
3 Replies

4. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

5. UNIX for Advanced & Expert Users

Help! SHELL or AWK script - only the masters of the forum will solve

Hello everybody! I have no experience with shell Programmer, but I need to compare 02 files. Txt and generate an output or a new file, after the comparisons. see: If the column 1 of file1 is equal to column 1 of file2, and column 3 of file2 contains the column 4 of file1, output: column1... (4 Replies)
Discussion started by: He2
4 Replies

6. Shell Programming and Scripting

shell script query

hi all i have a shell script for connecting in sybase env what i need is i have around 10 servers , i need to connect to all servers and retrive the database inforamtion in the servers and display them any one of u have it request to share it asap ! "QUERY TO Connect to all servers... (1 Reply)
Discussion started by: mvsramarao
1 Replies

7. Shell Programming and Scripting

Help me to solve some question about shell Script

Factorial calculation Example output: Please enter a non-negative number: 3 3! = 3 X 2 X 1 = 6 Please enter a non-negative number: 10 10! = 10 X 9 X 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 3628800 Please enter a non-negative number: -1 ... (1 Reply)
Discussion started by: cenco
1 Replies

8. Shell Programming and Scripting

Help me to solve some question about shell Script

Factorial calculation Example output: Please enter a non-negative number: 3 3! = 3 X 2 X 1 = 6 Please enter a non-negative number: 10 10! = 10 X 9 X 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 3628800 Please enter a non-negative number:... (1 Reply)
Discussion started by: cenco
1 Replies

9. UNIX for Dummies Questions & Answers

Shell script query

Hi, I am stuck assigning a value to a variable. I am trying to assign a value to a variable but getting error.... IP_ADDR=grep 'I.P. Address' /install/cfgdist/`uname -n`.cfg | cut -d : -f 2| cut -d . -f 1-3| sed s/" "//g I am using this script to grep first three octets of an IP address... (4 Replies)
Discussion started by: max29583
4 Replies

10. Shell Programming and Scripting

C Shell Script query

Hi I need a small help Cshell% more abc.txt ******** Cshell% cat abc.txt | cut -c1-3 *** Cshell%set test3=`cat abc.txt | cut -c1-3` Cshell%echo $test3 a.txt b.txt................. ..... It displays all the file in the current directory. I want *** to be displayed. Can any one of... (1 Reply)
Discussion started by: bpupdown
1 Replies
Login or Register to Ask a Question