Any shell scripts for cutting and pasting part of data?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Any shell scripts for cutting and pasting part of data?
# 1  
Old 03-17-2011
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.

Quote:
##Hello
##Welcome
#C1 C2 C3
1 1 1
2 2 2
3 3 3
3 3 3
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

Quote:
##Hello
##Welcome
#C1 C2 C3
3 3 3
3 3 3
1 1 1
2 2 2
Anyone knows any simple shell scripts to do that? The original file is too big so I want to just use shell scripts to process that data.

Thanks

-C
# 2  
Old 03-17-2011
A lame way of doing it:

Code:
sed '/^3/d' <yourdata.txt >temp1
sed '/^3/!d' <yourdata.txt >temp2
cat temp2 temp1 >result

# 3  
Old 03-17-2011
This answer is based on your original posting exact specifications.

Lines before line starting with ‘1’:
Code:
sed -n '/^1/q;p' inp_file > partI

Lines between line starting with '1' (inclusive) and starting with '3' (exclusive):
Code:
sed -n '/^3/q;/^1/,/^3/p' inp_file > partIII

Lines starting with '3':
Code:
egrep '^3' inp_file > partII

Put together:
Code:
cat partI partII partIII

# 4  
Old 03-17-2011
Thanks very much for both of you!
# 5  
Old 03-17-2011
one line awk:

bash code:
  1. awk -F'\t' 'BEGIN{i=1;}NR==FNR{if ($1~"^3") a[i++]=$0} NR>FNR{if (FNR<=3){print $0;next;} if ($1!~"^3")a[i++]=$0} END{for(j=1;j<i;j++) print a[j]; }' input input

output:

Code:
##Hello
##Welcome
#C1	C2	C3
3	3	3
3	3	3
1	1	1
2	2	2

# 6  
Old 03-17-2011
@sk1418, using line number is bad row with "1" in column 1 might not always be on row 3. Something like this would be better:

Code:
awk -F'\t' 'NR==FNR{if($1=="3")a[r++]=$0;next} r&&$1=="1"{for(i=0;i<r;i++)print a[i];r=0} $1!="3"' input input

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting a column and pasting its number

Hello Gurus, This is my first ever post here. I tried looking for similar material but came up empty handed. Apologies if this is too verbose or if I'm not using the correct formatting. I have files containing a fixed number of elements per line; separator is a single space. Each line has the... (4 Replies)
Discussion started by: stdroid
4 Replies

2. UNIX for Dummies Questions & Answers

Cutting only the 1st column data

Hi All, I having a data file which consists of 20cr records in it. The 1st column is year field which consist of year in format 200809 and fields are seperated with ^. How do i trim it to 2008 in file and save it in a quick time as there are many records so that i can use the file for loading... (3 Replies)
Discussion started by: abhi_123
3 Replies

3. Shell Programming and Scripting

Loading data in oracle using shell scripts

Hi , I have a scenario, i have a directory where i receive around 14-15 files at a interval of 20-40 min not fixed, i want to write a unix scripts which invoke sqlldr command to load files into oracle automatically as soon as the file hit the directory. Any help will be appreciated. ... (4 Replies)
Discussion started by: guddu_12
4 Replies

4. Shell Programming and Scripting

Cutting a part of line till delimiter

here are the few scenarios... isoSizeKB text NOT NULL, reserved1 varchar(255), KEY `deviceId` (`deviceId`) `d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci, `ePR` int(11) DEFAULT '0', PRIMARY KEY (`id`) ... (7 Replies)
Discussion started by: vivek d r
7 Replies

5. UNIX for Dummies Questions & Answers

[Q] cutting from one column and pasting to another

I have a file laid out in columns with the first two lines line being: 219 432 4567 219 432 4587 I need to create a single line command to cut the characters in the 5th column and paste them back to the first column in the same file. (Hint:Two good solutions exist, one in which you use a... (9 Replies)
Discussion started by: mcampos7
9 Replies

6. Shell Programming and Scripting

Shell Script Problems, Lose formatting when copy pasting from formatted file.

Hello, I'm having trouble with formatting some text via the terminal. I can get it perfectly formatted, but when I try and copy paste the text from the output file it loses it's formatting. Very frustrating! Basically I have 7 files (data data2 data3 data4 data5 data6 data7) containing a... (13 Replies)
Discussion started by: facetoe
13 Replies

7. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: shruthidwh
3 Replies

8. Shell Programming and Scripting

Cutting Part of Output

Hello all I'm using bourne shell and need to figure out how to cut out a specific portion of some output. For example, my output from my command is: 12.12.52.125.in-addr.arpa name = hostname.domain.main.gov I need to get just the "hostname.domain.main.gov" part. What I'm trying... (9 Replies)
Discussion started by: lee.n.doan
9 Replies

9. UNIX for Advanced & Expert Users

Bash shell: Cutting pasting only parts of the name of a directory into a variable

I have a script in a directory and want to search the directory before like follows: i=0 for file in ../HN_* do echo $file ((i+=1)) echo $i done Currently I get following output: ../HN_2 1 ../HN_3 2 (2 Replies)
Discussion started by: ABE2202
2 Replies

10. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies
Login or Register to Ask a Question