Help in adding text before columns in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in adding text before columns in shell script
# 1  
Old 07-29-2016
Help in adding text before columns in shell script

Hello,

Can someone please help in below requirement.
My requirement is to add date before to first column,some text before 1st,2nd coulmns and insert a new column in between 2 and 3 columns.

input file.

Code:
aa  123    dddd
aa  667    kdkdk
ddj 738    kkkk
aa  123    dddd
aa  667    kdkdk
aa  123    dddd
aa  667    kdkdk

expecting output file:

Code:
2016-07-29 06:23:12 type1=aa type2=123 ABC dddd
2016-07-29 06:23:12 type1=aa type2=667 ABC  djdkd
2016-07-29 06:23:14 type1=ddj type2=738 ABC kkkk
2016-07-29 06:23:15 type1=aa type2=123 ABC kdkdk
2016-07-29 06:23:17 type1=aa type2=667 ABC dddd

Thank You.

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 07-29-2016 at 02:35 AM..
# 2  
Old 07-29-2016
What have you tried so far?
# 3  
Old 07-29-2016
tried this

Code:
awk '{print "type1=" $1,"type2="$2,$3}'

but don't know how to add to date before 1st column and new column in 2 and 3rd.

tried this for date but not working.
Code:
DATE=$(date +"%m-%d-%Y")
awk'{$DATE,print "type1=" $1,"type2="$2,$3}'


Moderator's Comments:
Mod Comment Start using code tags, if you don't want to get banned by infractions, thanks.

Last edited by zaxxon; 07-29-2016 at 03:48 AM.. Reason: code tags
# 4  
Old 07-29-2016
You're not too far off. awk can't handle shell variables immediately. Did you consider awk's -v option to convey (shell variables') values into awk variables? Then, print the awk variable holding the date value just in front of the other fields.
# 5  
Old 07-29-2016
Your DATE is not correct, the time is missing. The new column can be just written, as you wrote any other text in your print. DATE can be handed over to awk with -v:

Code:
$ DATE=$(date +"%Y-%m-%d %H:%M:%S")
$ awk -v date="$DATE" '{print date, "type1=" $1, "type2="$2, "ABC", $3}' infile
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk
2016-07-29 09:23:56 type1=ddj type2=738 ABC kkkk
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk

This User Gave Thanks to zaxxon For This Post:
# 6  
Old 07-29-2016
Hello Cva2568,

You could make Zaxxon's command like as follows too, where we could mention the date within awk's variable itself.
Code:
awk -v date="$(date +"%Y-%m-%d %H:%M:%S")" '{print date, "type1=" $1, "type2="$2, "ABC", $3}' Input_file

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Adding Two Array in shell script

Hi Experts, I've got this problem where I need to add two array in shell script such that that is an element is greater than 9 then it get further split into individual digit, something like below : Input :- array1=(2 6 8 9 10 12) array2=(5 4 6 8 12 14) Output :- array3=(7 1 0 1 4 1 7 2 2... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

3. Shell Programming and Scripting

Adding text to the first line of a shell script

the first line of every unix script written in an interpreted language always has a "#!<path-to-the-language>" is there a way to include other text in that first line without it affecting the ability of the script to run??? for instance, if i change the following line: #!/bin/sh echo blah... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. UNIX for Dummies Questions & Answers

Solaris - Filter columns in text file and adding new column

Hello, I am very now to this, hope you can help, I am looking into editing a file in Solaris, with dinamic collums (lenght varies) and I need 2 things to be made, the fist is to filter the first column and third column from the file bellow file.txt, and create a new file with the 2 filtered... (8 Replies)
Discussion started by: jpbastos
8 Replies

5. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

6. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

7. Solaris

Creating script adding 3 different variables in 3 columns

I have 3 variables with different information.. they look like this (row-wise aswell): Variable1 = Roland Kalle Dalius Variable2 = ake123 ler321 kaf434 Variable3 = Richardsen Sworden Lokthar How can I sort them by variable3 alphabetical and add them into the same output so... (0 Replies)
Discussion started by: Prantare
0 Replies

8. Shell Programming and Scripting

Suggestions for adding columns to text file

Good afternoon to everyone, I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

9. Shell Programming and Scripting

Adding options to a shell script

I want to add options to my shell script but having problems, my code so far is; #!/bin/bash lflag= iflag= while getopts 'l:i:' OPTION do case $OPTION in l) lflag=1 lval="$OPTARG" ;;... (1 Reply)
Discussion started by: paulobrad
1 Replies

10. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies
Login or Register to Ask a Question