Visit Our UNIX and Linux User Community


How to Replace,Sort,and Append Character one script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to Replace,Sort,and Append Character one script
# 1  
Old 11-21-2007
How to Replace,Sort,and Append Character one script

Hi all
i am very new to shell scripting,hope u guys can help

i need to replace,sort and append character for the file that look like this:

1007032811010001000100000001X700026930409
1007032811010001000200000002X700026930409
1007032711020001000300000003X700026930409
1007032812010001000100000001X700026930409
1007032817010001000200000002X700026930409
1007032817010001000300000003X700026930409

into a file that look like this:

0
1
2007032711020001000300000003X700026930409
1
2007032811010001000100000001X700026930409
2007032811010001000200000002X700026930409
1
2007032812010001000100000001X700026930409
1
2007032817010001000200000002X700026930409
2007032817010001000300000003X700026930409


0 -----> refer (i) below
1 -----> refer (iii) below
2007032711020001000300000003X700026930409 -----> refer (ii,iii) below
1 2007032811010001000100000001X700026930409
2007032811010001000200000002X700026930409
1
2007032812010001000100000001X700026930409
1
2007032817010001000200000002X700026930409
2007032817010001000300000003X700026930409

basically i need to
i)add '0' for the first line
ii)replace first character if it is '1' to become '2'
iii)group the line based on the sorting (sort -k1.1n) and add one new line of '1' for each line that share the same first 14

how to do all these in one script?huhuuhuh

hope u guys can help(cuz i'm totally blank Smilie)

thanks in advance
# 2  
Old 11-21-2007
Code:
sort -n file|awk 'FNR==1{print 0}/^1/{sub(/1/,"2")}!x[substr($0,1,14)]++{print 1}1'


Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 11-21-2007
thanks radoulov,it works perfectly...many thanksssssssss!!!
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #370
Difficulty: Medium
Bytecode instructions are processed by hardware and so they may be arbitrarily complex.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

2. Shell Programming and Scripting

awk script to replace nth character with comma

I have a requirement as below. In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its... (3 Replies)
Discussion started by: aramacha
3 Replies

3. Shell Programming and Scripting

Bash script to replace a character with another

Hi. I'm a complete noob when it comes to scripting. I have approximately 2000 files scattered throughout different locations that I need to rename. The current files have a character, "." , that needs to be replaced with an underscore. I have no clue which route to go about correcting this.... (4 Replies)
Discussion started by: Nvizn
4 Replies

4. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

5. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

8. Shell Programming and Scripting

script to replace a character with '(Apostrophe)

Hi Friends, I need a sed or awk based script to replace a character(alphabet) with ' (Apostrophe). I tried to use the following two commands 1) sed -e 's/a/'/' filename 2) awk '{sub("a","'",$1);print}' filename but both got failed. Any help on this. Thanks in advance.. (3 Replies)
Discussion started by: ks_reddy
3 Replies

9. Shell Programming and Scripting

script command to replace character

Hi, i have log like this : Actually i want to change the time stamp, and the rule is like this : and My script is like this : I know the script will loop and loop again after 07:00 like this: Can somebody help me ?? Thanks in advance.. (5 Replies)
Discussion started by: justbow
5 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

Featured Tech Videos