1

I have file which is of the following format:

axel localhost> localhost>
mike localhost> STORAGE 298390/512000 (58.279296875%) localhost>
ab localhost> STORAGE 141361/512000 (27.6095703125%) localhost>
fghi localhost> localhost>
abcd localhost> STORAGE 165929/512000 (32.4080078125%) localhost>
lmno localhost> STORAGE 353073/512000 (68.9595703125%) localhost>
nuon localhost> localhost>
spar localhost> STORAGE 203766/512000 (39.798046875%) localhost>
fibo localhost> STORAGE 238204/512000 (46.52421875%) localhost>
nacci localhost> STORAGE 425737/512000 (83.1517578125%) localhost>
seldom localhost> STORAGE 69894/512000 (13.651171875%) localhost>
project localhost> STORAGE 86220/512000 (16.83984375%) localhost>
eccleston localhost> STORAGE 240084/512000 (46.89140625%) localhost>
swan localhost> STORAGE 279522/512000 (54.594140625%) localhost>

This is the output generated by listquota in cyradm (Cyrus-imapd). I have two questions;

  • In those lines which have localhost> localhost>, the first word should be written into another file and that line should be deleted.
  • For the other lines, the second, third and last columns need to be deleted.

How would I go about achieving this result?

Edit

Expected output

mike 298390/512000 (58.279296875%)
ab 141361/512000 (27.6095703125%)
abcd 165929/512000 (32.4080078125%)
lmno 353073/512000 (68.9595703125%)
spar 203766/512000 (39.798046875%)
fibo 238204/512000 (46.52421875%)
nacci STORAGE 425737/512000 (83.1517578125%)

My loop so far looks something like:

while read thisline; do
    if [ $2 == "localhost>" AND $3 == "localhost>" ];then
        echo $1 >>/root/names.txt
        sed '1d' filename.txt #but this deletes only the first line
    fi

done
4
  • 1
    So the expected output is...? And what have been your attempts so we can help you improve them? Commented Aug 15, 2013 at 11:10
  • @fedorqui - Sorry, edited question. Commented Aug 15, 2013 at 11:24
  • you are using $2 and $3 as awk would do, but it's not correct here. You should store these values and then check its content. Then the sed part should be deleting with sed '$line d' and keep increasing $line`. Anyway, I think Kent's answer goes very fast to the point so I would use that. Commented Aug 15, 2013 at 11:28
  • Almost every time you write a loop in a shell script you have the wrong approach. Most good shell solutions are a series of pipes. Loops usually are best implemented in awk, perl, etc. Also, shell is not an appropriate language for parsing text files - that's what awk was invented for and so is very good at it. Commented Aug 15, 2013 at 13:22

1 Answer 1

3

try this:

awk '/localhost> localhost>/{print $1 >"file2";next}{$2=$3=$NF=""}7' file

this will output text you want, and generate a "file2" for the localhost> localhost> case.

didn't test, I hope there is no typoes.

Sign up to request clarification or add additional context in comments.

8 Comments

what's the last 7 for?
@i.h4d35 you can use any number except 0 . I used 7 because I like 7.
@Kent What does the number do?
@Bernhard awk will do print if there is a True value. so it lets awk print the whole (modified) line.
@i.h4d35 glad to help. btw, you don't need cat file|awk '..code..' the cat is useless. just awk '..codes..' file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.