Storing the Column as Variable and Replacing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Storing the Column as Variable and Replacing
# 1  
Old 09-03-2013
Storing the Column as Variable and Replacing

Hi All,

Please suggest how can I achieve the below.

I have a file as below.

HTML Code:
CDRSCAN|124434|Loaded|S
Success|abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
Success|abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
Success|abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
Success|abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
I am trying to cut the first column and paste it in the Header record in the place of S as below.

HTML Code:
CDRSCAN|124434|Loaded| Success
abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
abc|xyz|123|xyz|abc|xyz|123|xyz|abc|xyz|123|xyz
Through cut command I am able to cut the first column value, but not sure how to store the value in a variable and place it in the Header Record.

Please help me with logic for achieving this.

Thanks & Regards, Deepti
# 2  
Old 09-04-2013
One way to do it is:
Code:
awk '
BEGIN { FS = OFS = "|" 
}
NR == 1 {
        $NF = ""
        printf("%s", $0)
        next
}       
NR == 2 {
        printf(" %s\n", $1)
}
{       printf("%s\n", substr($0, length($1) + 2))
}' file

On a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. UNIX for Dummies Questions & Answers

Storing all the PID's in a variable.

Hi, ps -ef|awk '{print $2}' i want to store the result of the above command in a variable. I never worked with arrays in shell scripting. i tried the below code: set a=`ps -ef|awk '{print $2}'` But echo $a returns null. I want to store the content in a variable and retrieve it... (2 Replies)
Discussion started by: pandeesh
2 Replies

3. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Storing a field within a variable

Hi, I need to figure out a way to to capture the contents of a field that is separated by a pipe sign. Example Data: -100F| some other description -10C| some description | some description As you can see, the length of the field on the left of the pipe can be any length and... (3 Replies)
Discussion started by: doza22
3 Replies

6. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

7. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

8. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question