Which cut command is more efficient?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Which cut command is more efficient?
# 15  
Old 03-28-2011
Quote:
Originally Posted by Corona688
cut has to read line by line. cat can just read and write huge blocks.
cat's behavior can be altered by supplying cmd. line flags so why would it read in chunks...block I/O on long lines would make its output binary.
# 16  
Old 03-28-2011
Quote:
Originally Posted by shamrock
cat's behavior can be altered by supplying cmd. line flags so why would it read in chunks...
Because it usually doesn't need to care about line boundaries. Stopping in the middle of a line won't make it "binary". (though cat without parameters ought to be binary-safe.) What blocks it does I/O in won't change the content of said I/O, a line too long for the block will just take a couple blocks to finish.
This User Gave Thanks to Corona688 For This Post:
# 17  
Old 03-28-2011
Quote:
Originally Posted by Corona688
Because it usually doesn't need to care about line boundaries. Stopping in the middle of a line won't make it "binary". (though cat without parameters ought to be binary-safe.) What blocks it does I/O in won't change the content of said I/O, a line too long for the block will just take a couple blocks to finish.
You are right...I was thinking about reading the file into a struct whose padding may get junk chars being output...but I was wrong. However here is a link to a recent version of cat and it does process cmd. line with and without flags differently.
# 18  
Old 03-28-2011
Hi.

Along this line, here is something similar to an exercise I usually had students do:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate cat copying a system executable file.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C cat

# Remove debris, list current situation..
pl " Current situation:"
rm -f f1
/bin/ls -lgG

# Copy executable with cat, look at type, make it executable.
pl " New file characteristics:"
cat /bin/ls > f1
file f1
chmod +x f1

# Run it.
pl " Results of executing copy of file:"
./f1 -lgG

# Compare the files with cmp.
pl " Results of comparison:"
if cmp --quiet /bin/ls f1
then
  pe " Files are the same according to cmp."
else
  pe " Files differ."
fi

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
cat (GNU coreutils) 6.10

-----
 Current situation:
total 4
-rwxr--r-- 1 836 Mar 28 16:44 s1

-----
 New file characteristics:
f1: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, stripped

-----
 Results of executing copy of file:
total 108
-rwxr-xr-x 1 101992 Mar 28 16:45 f1
-rwxr--r-- 1    836 Mar 28 16:44 s1

-----
 Results of comparison:
 Files are the same according to cmp.

This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficient way to combine command

im currently running the following command to grab all arguments in front of a script, directly from the process table. # cat /tmp/allmyprocs ubuntu 9933 27793 0 03:29 pts/0 00:00:00 /bin/sh ./prying.sh ubuntu 9941 9933 0 03:29 pts/0 00:00:00 sh ubuntu 9952 9941 0 03:29... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Combining awk command to make it more efficient

VARIABLE="jhovan 5259 5241 0 20:11 ? 00:00:00 /proc/self/exe --type=gpu-process --channel=5182.0.1597089149 --supports-dual-gpus=false --gpu-driver-bug-workarounds=2,45,57 --disable-accelerated-video-decode --gpu-vendor-id=0x80ee --gpu-device-id=0xbeef --gpu-driver-vendor... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

4. Shell Programming and Scripting

Cut command

hi, i have a file abc,"an,ab",cde,efg abc,anab,cde,efg and need to cut the second field so the output should be abc,cde,efg and i have used cut -d',' -f1-1,3- but its giving me abc,ab",cde,efg abc,cde,efg (4 Replies)
Discussion started by: ATWC
4 Replies

5. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

6. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

7. Shell Programming and Scripting

Help with cut command

Gurus, I need help with the cut command : I have a file with garbage charaters at the beginning of each record; but these characters are not of the same length; First record has 3 garbage chars to be removed; rest have 2; If the length was consistent across all the records, I could have... (3 Replies)
Discussion started by: tru_tell
3 Replies

8. Shell Programming and Scripting

Cut command

Hi, I want to cut from a particular position to a particular position and retain the rest. I tried this cut -c31-51 file1.txt > file2.txt But The characters from the position 31 to 51 were only present in file2.txt. Is there a way to reverse this i.e to retain the rest except from... (1 Reply)
Discussion started by: ragavhere
1 Replies

9. UNIX for Dummies Questions & Answers

cut command

how do you show just the used disk space. using the cut and df command?? or does anyone have any other suggestions on how to do it a better way? (3 Replies)
Discussion started by: rookie22
3 Replies
Login or Register to Ask a Question