Romove columns and replace a space with a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Romove columns and replace a space with a character
# 1  
Old 05-07-2013
Romove columns and replace a space with a character

Hi, I have a file containing this:

Code:
testvol1        unix enabled
testvol2        unix enabled
testvol3        unix enabled
testvol3 qtree1 unix enabled
testvol3 qtree2 unix enabled
testvol4        unix enabled
testvol4 qtree1 unix enabled

And I want an output of this:
Code:
testvol1
testvol2
testvol3
testvol3\qtree1
testvol3\qtree2
testvol4
testvol4\qtree1

Tried awk, but I dont know how to exclude the 3rd 4th column when nothing is in the second column, and dont know how to replace the space between column 1 and 2 with a “\”.


Random lines could have a blank 2nd column, so cannot specify specific lines.
# 2  
Old 05-07-2013
Code:
awk '{print $1 ((NF==4)?"\\"$2:"")}' myFile


Last edited by vgersh99; 05-07-2013 at 02:27 PM.. Reason: misread the req
# 3  
Old 05-07-2013
Code:
awk '{$0=NF==3?$1:$1 OFS $2}1' OFS='\' file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 05-07-2013
Not sure if it could be any shorter than this Smilie
Code:
awk 'NF-=2' OFS='\' file
testvol1
testvol2
testvol3
testvol3\qtree1
testvol3\qtree2
testvol4
testvol4\qtree1

These 2 Users Gave Thanks to Jotne For This Post:
# 5  
Old 05-07-2013
Thanks guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. 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

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Shell Programming and Scripting

Replace newline character between a double quotes to a space

Hi Guys, I have a file with content as below aj.txt "Iam allfine" abcdef abcd "all is not well" What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text. output must be:... (8 Replies)
Discussion started by: ajahuja
8 Replies

6. 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

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

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

9. Shell Programming and Scripting

How to replace all entries of comma in text file by space or other character

Hi , How to replace all entries of comma in text file by space or other character. cat temp.txt A,B,C,D I want this file to be like A B C D Please help!!! (4 Replies)
Discussion started by: prashant43
4 Replies

10. AIX

How can i replace a character with blank space?

i want a command for my script!!! say file consists of character 123 125 127. i need a query to replace the number 2 with 0 so the output should be 103 105 107. i use unix-aix (8 Replies)
Discussion started by: rollthecoin
8 Replies
Login or Register to Ask a Question