Visit Our UNIX and Linux User Community


sed within awk statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed within awk statement
# 1  
Old 01-09-2018
sed within awk statement

Code:
 
input

| Jan 8 2018 11:28PM| 24 | 75 | 51 | 1 | 1.600|
| Jan 8 2018 12:01PM| 52 | 823 | 21 | 6 | 2.675|

Code:
 
desired output

Jan-8-2018-11:28PM     24     75     51     1     1.600
Jan-8-2018-12:01PM     52     823   21     6     2.675

Dear friends,

I have input file , as shown above and I want to convert it into output, as shown above.
kindly let me know how to use sed command inside awk statement so that changes can be made.

the output is text file and output is to be aligned or formatted as table.


As this input file is received from MySQL query, and when it is converted to .csv file some columns file format gets changed ( column 2 and 3 are treated as general and is shown in decimal but they are numbers). Hence is it possible to set column format in unix for .csv file , so that I will not need it to save as .txt and can directly saved as .csv with ","separated file

thanks in advance

Moderator's Comments:
Mod Comment edit by bakunin: changed QUOTE-tags to CODE-tags. Please use these (not QUOTE-tags) for code, data and terminal output as they will not eat away white space and maybe preserve information which otherwise would be lost. Thank you.

Last edited by bakunin; 01-10-2018 at 05:54 AM..
# 2  
Old 01-09-2018
What have you tried?
# 3  
Old 01-09-2018
And, why haven't you written your MySQL query to directly produce output in the format you want?
# 4  
Old 01-10-2018
I have tried to do it in sql server 2012 but the column length is flexible and I have also tried to get date format as desired from 100 to 114

now I am doing this

Code:
 
 convert(varchar(19),call_start_time,100)

so that I can directly run awk command to remove spaces from my file.
but its not working.
i have also searched on net regarding this sed inside awk command and i came to know about gsub function but i am stuck in between.
Hence kindly help me, how to figure this out
# 5  
Old 01-10-2018
So you want to do an SQL query followed by an awk script followed by a sed script. Hmmm - given the enormous formatting capabilities of usual SQL implementations, this doesn't seem too efficient.
MySQL provides the DATE_FORMAT(date, format_mask) function which would allow an immediately correct output. And the COLSEP (if I remember correctly) gives you control over the column separator character (e.g. ; ).
# 6  
Old 01-10-2018
I'm not an SQL expert, but still believe this could all be done in SQL without needing awk or sed. And, you showed us the output you said you want with <space> as a field separator and then say that you want the output to be a CSV file with <comma> as the field separator??? Assuming that your input is in a file named file and that you do want output with <comma> as the field separator, the following awk script seems to do what I think you want:
Code:
awk -F'|' '			# Invoke awk and set input field separator to "|".
{	gsub(/^ | $/, "", $2)	# Get rid of leading and trailing space in date
				# and convert input FS (|) to output FS (space).
	gsub(/ /, "-", $2)	# Convert spaces to hyphens in date.
	gsub(/ +/, ",")		# Convert strings of adjacent spaces to commas.
	gsub(/^,|,$/, "")	# Get rid of leading and trailing commas.
	print			# Print updated lines.
}' file				# Close awk script and specify input file.

If file contains the input you showed us in post #1 in this thread, this produces the output:
Code:
Jan-8-2018-11:28PM,24,75,51,1,1.600
Jan-8-2018-12:01PM,52,823,21,6,2.675

You haven't told us what operating system or shell you're using (which you should always do in threads in this forum so we can suggest solutions that will work in your environment). If you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-10-2018
I have already tried DATE_FORMAT(date, format_mask) function but it gives error

Code:
 
 'DATE_FORMAT' is not a recognized built-in function name.

Details of database are as below.

Code:
 
 Microsoft SQL Server Management Studio      11.0.2100.60
Microsoft Analysis Services Client Tools      11.0.2100.60
Microsoft Data Access Components (MDAC)      6.3.9600.16384
Microsoft MSXML      3.0 6.0 
Microsoft Internet Explorer      9.11.9600.17690
Microsoft .NET Framework      4.0.30319.34014
Operating System      6.3.9600

The sample input given, is output of query and I want to format it as given above.


Is it possible to use sed within awk statement or please let me how to do it with gsub function to get desired output.

Previous Thread | Next Thread
Test Your Knowledge in Computers #479
Difficulty: Medium
A address usually refers to a single byte of memory (called bitwise-addressing).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk statement piped inside sed

Hello folks, I have multiple occurrences of the pattern: ).: where is any digit, in various text context but the pattern is unique as this regex. And I need to turn this decimal fraction into an integer (corresponding percent value: the range of 0-100). What I'm doing is: cat... (1 Reply)
Discussion started by: roussine
1 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

If else statement in sed

Hello Guys, I am new here and this is my first post, hope someone can help me I am writing a script that is supposed to go in 9 different directories and edit a given file in each of the directories. I am using sed to edit the file as sed -i 'line# s/#to be changed/#to be replaced with/... (5 Replies)
Discussion started by: Madiouma Ndiaye
5 Replies

4. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

5. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

6. Shell Programming and Scripting

A complex sed statement

I have following requirement. Say, my text file contains following patterns {2010501005|XXGpvertex|9|0|17|0|{|{30100001|XXparameter_set|@@@@{{30001002|XXparameter|!prototype_path|$AB_COMPONENTS/Sort/Sort.mpc|3|2|Pf$|@{0|}} }}@0|@315000|78500|335000|99000|114000|87000|17|And the Sort|Ab... (8 Replies)
Discussion started by: Shell_Learner
8 Replies

7. Shell Programming and Scripting

sed / grep / for statement performance - please help

I'm searching the most effective way of doing the following task, so if someone can either provide a working solution with sed or one totally different but more effective then what I've got so far then please go ahead! The debugme directory has 3 subdirectorys and each of them has one .txt file... (7 Replies)
Discussion started by: TehOne
7 Replies

8. Shell Programming and Scripting

sed remove statement

I am having some problems with sed, that I am hoping that I can get some assistance with. I am trying to remove two subsets of a string, and cannot figure out how to have it work. Here is an example string: auth_ldap authenticate: user joe authentication failed; URI /svn/ I want to... (4 Replies)
Discussion started by: Guyverix
4 Replies

9. Shell Programming and Scripting

Variables within a sed statement

I am just wondering if it's possible to refer to variables within a sed statement as follows:- cat $file | sed -e 1's/$oldtext/$newtext/' > $file as when I run the script, the variables are not recognised and nothing happens..?? Thanks (5 Replies)
Discussion started by: sirtrancealot
5 Replies

10. Shell Programming and Scripting

if and sed statement

this is my output for my crawler. /about.html /ads/ /advanced_search?hl=en froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8 groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8 /imghp?hl=en&tab=wi&ie=UTF-8 /intl/en/options/ /language_tools?hl=en /maphp?hl=en&tab=wl&ie=UTF-8... (3 Replies)
Discussion started by: chris1234
3 Replies

Featured Tech Videos