Replace field when only "-" occurs on a random basis


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace field when only "-" occurs on a random basis
# 1  
Old 12-03-2009
Replace field when only "-" occurs on a random basis

I have a file in which "-"(by itself and nothing else) occurs in different fields in each record(sometimes in the 3 field, sometime in the 20th field, some time in the 100th field....etc). Also there are field of the format "abc-def". How do I replace fields that have just "-" with number zero. sed replaces all the "-", like "abc-def" would become "abc0def", I don't want that to happen.
# 2  
Old 12-03-2009
What are the fields delimited by?
# 3  
Old 12-04-2009
its a tab delimited file. I am trying to use gawk to replace the "-"
# 4  
Old 12-04-2009
I've never used gawk, but sed can do the trick.

Code:
cat blah
abc-def -       ghi-jkl -       mno-pqr

sed 's/\t-\t/\t0\t/g' blah.dat > new_blah.dat

cat new_blah.dat
abc-def 0       ghi-jkl 0       mno-pqr

# 5  
Old 12-04-2009
Thanks!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

3. Shell Programming and Scripting

Mailx is adding a random "!" in the middle of my mail

I made a script that sends an email with one long line, line looks like this. UY453;HJ345;BM342;JH526;HJ245;HJ234;NM235;........JK453;SD245; The mail has about 200 of those IDs in one line, each ID is separtated by ";". The problem is that there is a random exclemation mark and it... (3 Replies)
Discussion started by: ajetangay
3 Replies

4. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

5. UNIX for Advanced & Expert Users

Should I say "field 8" or "column 8" in this case?

I saw some recent posts where I thought the terms "field" and "column" were being misused. I work with data a lot, and have my opinions. I'm wondering if those opinions are correct. ***** Rows seem clear - I don't think there is any controversy about what a row is, either for database or text... (10 Replies)
Discussion started by: hanson44
10 Replies

6. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

How to replace "®" "™" "with Spaces in UNIX

do U know how to replace the registered trademark "®" symbol or trade Mark "™" "with Spaces in UNIX (4 Replies)
Discussion started by: MMeari
4 Replies

9. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

10. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies
Login or Register to Ask a Question