sed problem with a table file <solved> bypass sed for awk

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#21 Post by jpeps »

seaside wrote:8-bit,

Here's the all awk version eliminating the first line.

Code: Select all

ps -A | awk 'FNR>1{print $1"|"$2"|"$3"|"$4"|"}'
Cheers,
s
unlike sed, awk doesn't seem to need (or like) escapes. The only char that seems to need it is the escape itself "\"

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#22 Post by potong »

ps is a wonder!

Code: Select all

# ps -A|head -5
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:01 ksoftirqd/0
    4 ?        00:00:00 migration/0
# # elimiate headers
# ps -A --no-headers|head -5
    1 ?        00:00:00 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:01 ksoftirqd/0
    4 ?        00:00:00 migration/0
    5 ?        00:00:00 migration/1
# # re-arrange the headings
# ps -eo cmd,time,tty,pid|head -5
CMD                             TIME TT         PID
init                        00:00:00 ?            1
[kthreadd]                  00:00:00 ?            2
[ksoftirqd/0]               00:00:01 ?            3
[migration/0]               00:00:00 ?            4
# # another way
# ps -eo '%y%a%p%t'|head -5
TTY      COMMAND                       PID     ELAPSED
?        init                            1    10:44:41
?        [kthreadd]                      2    10:44:41
?        [ksoftirqd/0]                   3    10:44:41
?        [migration/0]                   4    10:44:41
# # yet another way
# PS_FORMAT='%p%y%t "%a"' ps -e|head -5
  PID TTY          ELAPSED "COMMAND                    "
    1 ?           10:45:50 "init                       "
    2 ?           10:45:50 "[kthreadd]                 "
    3 ?           10:45:50 "[ksoftirqd/0]              "
    4 ?           10:45:50 "[migration/0]              "
Bash can make your head ache, but it's a good kind of ache!
Where were we...

Code: Select all

# while read -r line
> do read -r a b c d <<<"$line"
> printf "%s|%s|%s|%s\n" "$a" "$b" "$c" "$d"
> done < <(ps -eo "%p%y%t%a") |
> head -5
PID|TTY|ELAPSED|COMMAND
1|?|11:02:14|init
2|?|11:02:14|[kthreadd]
3|?|11:02:14|[ksoftirqd/0]
4|?|11:02:14|[migration/0]
Read the 'ps' command line by line, Word split each $line using read from a here-string (first 3 cols go in $a,$b,$c remainder in $d).
Use these as arguments for 'printf' (four at a time till they're all used up).

That's OK but we can do better!
If we pre-format the 'ps' output and double-quote the space ridden arguments column, we can use bash to word-split the whole line:

Code: Select all

# printf "%s|%s|%s|%s\n" $(PS_FORMAT='%p%y%t "%a"' ps -e)| head -5
PID|TTY|ELAPSED|"COMMAND
"|1|?|11:59:50
"init|"|2|?
11:59:50|"[kthreadd]|"|3
?|11:59:50|"[ksoftirqd/0]|"
Oops! Word-splittings not working!
Or rather, it's falling short of splitting the double-quoted arguments column. It needs a bit more 'Umph'... lets 'eval' and see:

Code: Select all

# eval printf "%s|%s|%s|%s\n" $(PS_FORMAT='%p%y%t "%a"' ps -e)|        
> head -5
bash: %s: command not found
bash: %s: command not found
bash: %sn: command not found
Bother! 'printf' getting in the way... Oh well lets hide it in a function:

Code: Select all

# p(){ printf "%s|%s|%s|%s\n" "$@"; }; eval p $(PS_FORMAT='%p%y%t "%a"' ps -e)|        
> head -5
PID|TTY|ELAPSED|COMMAND 
1|?|12:06:33|init 
2|?|12:06:33|[kthreadd] 
3|?|12:06:33|[ksoftirqd/0] 
4|?|12:06:33|[migration/0] 
Ta-daa, that's better!
But could we have just used?:

Code: Select all

# ps --no-headers -eo "%p|%y|%t|%a" | head -5
    1|?       |   12:09:39|init      
    2|?       |   12:09:39|[kthreadd]
    3|?       |   12:09:39|[ksoftirqd/0]
    4|?       |   12:09:39|[migration/0]
    5|?       |   12:09:39|[migration/1]
BTW 'sed -r' gets rid of a lot of toothpicks.

HTH

Potong

p.s. Now if I can just fix bash history so I can see the last here-document I typed in!

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#23 Post by big_bass »

this were you see the Spaghetti meets the plate

awk has a nice clean way of printing out arrays

the tr command is very simple and fast

there is another important factor you cant overlook and that is speed
if you have to parse large files speed is very important

fractions of seconds turn into seconds and maybe minutes
making the GUI hang until things settle

if you run the examples posted and time them who is the fastest?

but then again more was explained having different examples
anyone of these examples may have a key part in your next script
so all is well that ends well

its as always sorting through a lot of jargon to get to the heart of the matter

Leonardo da Vinci
“Simplicity is the ultimate sophistication.

Post Reply