data searching and pasting with in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting data searching and pasting with in the file
# 1  
Old 05-03-2011
data searching and pasting with in the file

Hi All,

I have .csv file in which I am trying to manipulate a column date, I started with awk but i am not sure how to do the below logic in .

the file has a 23 columns and in the first row if the value is Trend and in the second column the value is Analysis then the program has to search(with in the file) in the column starting with INS_FLAG and analysis combination in first and second places and see if either of the value is present i.e SINGLE or MULTIPLE( only these combinations will be there) then it has to take this value and write it in the 6th column data where the first column value is trend and second column data is analysis.

I know it's confusing I am posting the sample data below.

source

Code:
INS_FLAG,Analysis,,, single,,,
INS_FLAG,ABC,,, NA,,,
INS_FLAG,XYZ,,, ABC,,,
trend,Analysis,,,/abc/xyz/runtime,,,
trend,abc,,,/abc/xyz/runtime,,,

target

Code:
INS_FLAG,Analysis,,, single,,,
INS_FLAG,ABC,,, NA,,,
INS_FLAG,XYZ,,, ABC,,,
trend,Analysis,,,/abc/xyz/single,,,
trend,abc,,,/abc/xyz/runtime,,,

Please give me a hint, how can I implement this in unix.

Thanks,Shruthi
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-03-2011 at 10:58 AM.. Reason: code tags, please!
# 2  
Old 05-03-2011
Well, you need to be a little more clear.

Input file
Code:
INS_FLAG,Analysis,,, single,,, 
INS_FLAG,ABC,,, NA,,, 
INS_FLAG,XYZ,,, ABC,,, 
trend,Analysis,,,/abc/xyz/runtime,,, 
trend,abc,,,/abc/xyz/runtime,,,

What do you mean by column? Is it comma (,) separated?
Your output has the line
Code:
trend,Analysis,,,/abc/xyz/single,,,

where "runtime" is replaced with "single"

yeah, we are confused.
Help us to help you.

regards,
Ahamed
# 3  
Old 05-03-2011
Hi Ahamed,

I am referring column as a comma separated value, and the value of 6th column is like line, I need to pick the value from the
INS_FLAG,Analysis,,, single,,, and replace the single with runtime
trend,Analysis,,,/abc/xyz/runtime,,,

Thx,Shruthi
# 4  
Old 05-05-2011
Try this

Code:
#with awk
awk -F, '/INS_FLAG,Analysis/ {val=$5} /trend,Analysis/ {sub(/runtime/,val,$0)} {print}' file > newfile

With the script
Code:
#!/bin/ksh
line=$( grep -n "trend,Analysis" file )
if [ ! -z "$line" ]
then
  lineNum=$( echo $line | cut -f1 -d":" ) 
  lineData=$( echo $line | cut -f2 -d":" ) 
  newData=$( grep "INS_FLAG,Analysis" file | awk -F, '{print $5}' )
  lineDat=$( echo $lineData | sed "s!runtime!$newData!g" ) 
  sed -i "${lineNum}d" file
  sed -i "${lineNum} i $lineDat" file
fi

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch the different data by searching with a same variable from a file in AIX server

Hi, I am trying to fetch the different values in an xml file by searching with the same variable in AIX Server. <name>SharedResources/Shared/JNDI/Username</name> <value>admin</value> <name>SharedResources/Shared/JNDI/Username</name> ... (1 Reply)
Discussion started by: tejastrikez
1 Replies

2. Shell Programming and Scripting

reading files and pasting in another text file

Hi all, I have certain task to do, which involves reading the first column of 1.txt file. This is variable "event" 28434710 23456656 3456895 & finding this "event" in some other text file 2.txt, which has information in the following format #Zgamma: 1 run: 160998 event: ... (7 Replies)
Discussion started by: nrjrasaxena
7 Replies

3. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

4. UNIX for Dummies Questions & Answers

Copying and Pasting columns from one text file to another

I have a tab delimited text file that I want to cut columns 3,4,5 from. Then I want to paste these columns into a space delimited text file between columns 2 and 3. I still want to keep the space delimited format in the final text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Pasting a column into a text file

I have two text files: One is a single column of numbers and the other is a space delimited text file with multiple columns. I want to paste the single column of numbers into the second column of the latter text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Any shell scripts for cutting and pasting part of data?

Hi, I have a tab-delimited txt file as below. It is part of the original file. I want to cut the lines starting with "3" in column1 and paste them before the lines starting with "1" in column 1. So I will get Anyone knows any simple shell scripts to do that? The original file is... (5 Replies)
Discussion started by: cliffyiu
5 Replies

7. Shell Programming and Scripting

Searching data files for another file of values

I've used awk for some simple scripting, but having trouble figuring out how to search a couple of data files that have Name/Address/Zip Codes from another file that has list of only Zip Codes, and write out the lines that matched. Zip code field in the data file is 27 I was thinking... (5 Replies)
Discussion started by: matkins99
5 Replies

8. UNIX for Dummies Questions & Answers

Preserve byte size of fields while pasting it to other file

Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be preserved.i.e. If I want to append content of ABC.txt to other... (0 Replies)
Discussion started by: dashing201
0 Replies

9. Shell Programming and Scripting

Searching a particular string with spaces in a data file

Hi, I'm new to shell scripting and require your help in achieving the requirement. I have a data file which stores organization name as one of the column data in a csv data file. Organization name stored in data file is: Canadian OU CAD Sample Data file: 1,5,4,5,, ... (9 Replies)
Discussion started by: andy4013
9 Replies

10. UNIX for Dummies Questions & Answers

pasting text into an existing file

I have a text file that has over 300 lines that I need to paste identical data to. What is the easiest way to do this? For example if I have a file that has lines of text xxxxxxxx and I would like to change each line to look like this "display text(xxxxxxxx)". What would be the easiest way to do... (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question