Conditional tab replacement sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional tab replacement sed/awk
# 1  
Old 05-17-2010
Conditional tab replacement sed/awk

Hi

I am struggling to find a solutions to this problem:

I have a directory full of files and I wish to:

read each line of each file and

if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character.

Ive been researching/testing sed and awk but cant get anything working in this manner.


Any help would be greatly appreciated.

Thanks!
# 2  
Old 05-17-2010
Something like this?
Code:
awk 'length > 72 {gsub("\t"," ")}1' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-17-2010
Exactly like that :-) Thanks very much!

I'm new to all this, I appreciate the help.

How do I make those changes to the file itself rather than printing the formatted text to the screen?
# 4  
Old 05-17-2010
Quote:
Originally Posted by benackland
Exactly like that :-) Thanks very much!

I'm new to all this, I appreciate the help.

How do I make those changes to the file itself rather than printing the formatted text to the screen?
You can't edit files in place with awk, use a temporary file:
Code:
awk 'length > 72 {gsub("\t"," ")}1' file > tempfile
mv tempfile file

or maybe with something like:
Code:
awk 'length > 72 {gsub("\t"," ")}{print > "tempfile"}
END{system("mv tempfile file")}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed or awk Replacement/Addition

Hi, I have a big text file with similar data as below and need the text as in output using awk or sed. any help is greatly appreciated. Input: City=Chicago Elden street >>> reservedBy = business 1 >>> reservedBy = business 2 >>> reservedBy = business 3 City=Dallas Elm street >>>... (5 Replies)
Discussion started by: tech_frk
5 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

4. Shell Programming and Scripting

Conditional replacement in CSV files

Hello, I have many CSV files with variable number of rows and columns. Sample of few problematic CSV files. ,,Price,Price,Price,Price,Price,Price,Price,Price,Price,Qty Date,Sl,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,PriQueue,%busy 30/07/2014,1,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,NA,0... (8 Replies)
Discussion started by: reddyr
8 Replies

5. Shell Programming and Scripting

Conditional replacement of columns in a text file

Hello scriping expert friends, I have 2 requirements on replacing fields of text files: I have lot of data with contents like below: Requirement-1: The digit after 0 should always be changed to 1 (3 Replies)
Discussion started by: magnus29
3 Replies

6. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

7. Shell Programming and Scripting

Conditional replacement of a delimiter

Hello, I'm new to this forum but this seems like the place to ask this question. I have a pipe delimited data file with the fields except for the header being encased in double quotes. I found out that some of the fields have an trash pipe within the data itself. I'd like to conditionally... (4 Replies)
Discussion started by: samahs
4 Replies

8. Shell Programming and Scripting

conditional replacement

Hi all, I need a bash, sed, awk script or one liner to do the following task: This is the format of a text file: 2010-06-11 20:01 902656 HOP-W-100412-1.doc 2010-11-05 18:01 364447 NEX-W-101104-1 2010-07-06 10:01 64512 Cerintele 2010-07-06 10:01 599420 content 2010-07-19 14:01 1785344... (7 Replies)
Discussion started by: supervazi
7 Replies

9. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies

10. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies
Login or Register to Ask a Question