345

For example, I might want to:

tail -f logfile | grep org.springframework | <command to remove first N characters>

I was thinking that tr might have the ability to do this but I'm not sure.

Sam
  • 7,252
  • 16
  • 46
  • 65
les2
  • 14,093
  • 16
  • 59
  • 76

7 Answers7

495

Use cut. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):

tail -f logfile | grep org.springframework | cut -c 5-
iammichael
  • 9,477
  • 3
  • 32
  • 42
  • 1
    do you have any idea of why the pipe doesn't work? when i run essentially that command, 'cut' doesn't print the results to stdout ... if i just run 'tail -f logfile | cut -c 5-' i can see the results ... the problem must be with grep i'm using cygwin FYI thanks – les2 Jun 09 '09 at 19:15
  • what does happen if you do not add the last pipe and cut ? basically, if you remove the last part of the line ? – LB40 Jun 09 '09 at 19:21
  • it "tails" the logfile, filtering it with grep (i.e., all of the lines with "org.springframework" in them are printed to stdout) when i add the pipe to 'cut' ... it hangs HOWEVER, if i eliminate 'grep' the 'cut' works properly ... i'm thinking something's wrong with how i'm using grep ... could be a cygwin thing too – les2 Jun 09 '09 at 19:31
  • 1
    oh i see...why do you use tail ? just do grep org.springframework logfile | cut -c 5- but i think sed is nicer :-) – LB40 Jun 09 '09 at 19:57
  • because the '-f' arg to 'tail' causes tail to continuously read the file ... it allows me to view updates to the logfile as they are written – les2 Jun 09 '09 at 20:10
  • @les2: probably way to late, but if grep seems to be the culprit why not swap the pipes to: `tail -f logfile |cut -c 5-|grep org.springframework` ? – Alexander Oh Mar 22 '13 at 10:07
  • 8
    The problem is, that grep is buffering big chunks before sending them to cut because it can see that it's not writing to a terminal. Use `grep --line-buffered "org.springframework` to solve that issue. – Steen Schütt Jul 07 '14 at 22:25
  • what is the - char after the 5-? why – Alexander Mills Dec 22 '18 at 03:31
  • what if we want to perform the operation on only 1 line not each and every line ?? – Sandy Dec 01 '21 at 14:32
64
sed 's/^.\{5\}//' logfile 

and you replace 5 by the number you want...it should do the trick...

EDIT if for each line sed 's/^.\{5\}//g' logfile

LB40
  • 12,041
  • 17
  • 72
  • 107
51

You can use cut:

cut -c N- file.txt > new_file.txt

-c: characters

file.txt: input file

new_file.txt: output file

N-: Characters from N to end to be cut and output to the new file.

Can also have other args like: 'N' , 'N-M', '-M' meaning nth character, nth to mth character, first to mth character respectively.

This will perform the operation to each line of the input file.

jshoo
  • 3
  • 2
Ankur
  • 5,613
  • 1
  • 15
  • 14
7
x=hello

echo ${x:1}

returns ello

replace 1 with N as required

Mark
  • 1,337
  • 23
  • 34
7
tail -f logfile | grep org.springframework | cut -c 900-

would remove the first 900 characters

cut uses 900- to show the 900th character to the end of the line

however when I pipe all of this through grep I don't get anything

Nakilon
  • 34,866
  • 14
  • 107
  • 142
les2
  • 14,093
  • 16
  • 59
  • 76
6

Here is simple function, tested in bash. 1st param of function is string, 2nd param is number of characters to be stripped

function stringStripNCharsFromStart {
    echo ${1:$2:${#1}}
}

Usage:

$ stringStripNCharsFromStart "12abcdefgh-" 2
# 2abcdefgh-

Screenshot:

Screenshot of result from console

KyleMit
  • 30,350
  • 66
  • 462
  • 664
To Kra
  • 3,344
  • 3
  • 38
  • 45
4

I think awk would be the best tool for this as it can both filter and perform the necessary string manipulation functions on filtered lines:

tail -f logfile | awk '/org.springframework/ {print substr($0, 6)}'

or

tail -f logfile | awk '/org.springframework/ && sub(/^.{5}/,"",$0)'
John B
  • 3,566
  • 1
  • 16
  • 20