I want to print out only a certain line from the output of a command.
Lets take the example of ipconfig command. This returns a lot of lines.
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ab80::456d:123e:5ae5:9ab6%15
IPv4 Address. . . . . . . . . . . : 192.168.1.33
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
Ethernet adapter Local Area Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
I want to just print say the 11th line.
I tried the following
FOR /F "skip=10 delims=" %G IN ('IPCONFIG') DO #ECHO %G
This skips only the first 10 lines and prints the rest of the lines.
Default Gateway . . . . . . . . . : 192.168.1.1
Ethernet adapter Local Area Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
How do I print only the 11th line?
Just leave the loop
FOR /F "skip=10 delims=" %G IN ('IPCONFIG') DO #ECHO %G & goto done
:done
edited Get the 11th line in the output of a command from a single command line
for /f "tokens=1,* delims=:" %a in ('ipconfig^|findstr /n "^"^|findstr /l /b /c:"11:"') do echo %b
Execute the command, number the output, retrieve the required line, split the initial number and echo the rest
set "x=1" & for /f "skip=10 delims=" %a in ('ipconfig') do #(if defined x (set "x=" & echo %a))
Set a flag variable, execute the command, skip the first 10 lines and for each line if the flag is set, clean the flag and echo the line
Could I run this in a single command? I mean not through a batch file. yes:
ipconfig |find "Default Gateway"
(runs both on the command line and in a batch file)
Related
In This Question a script is presented to capture the last default gateway. (Another solution is shown to capture the first gateway, but I want the last one.
The script is:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do set ip=%%b
set ip=%ip:~2%
echo.
echo The Gateway is: %ip%
echo.
Given this ipconfig input (some details removed for privacy):
Ethernet adapter Ethernet 2:
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 10.1.251.148
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 0.0.0.0
Ethernet adapter Npcap Loopback Adapter:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::****:a19b:****:bf4%15
Autoconfiguration IPv4 Address. . : 169.254.11.244
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . : ev***.li****.io
Link-local IPv6 Address . . . . . : fe80::a**:c4**:c**1:dfa3%14
IPv4 Address. . . . . . . . . . . : 10.100.17.34
Subnet Mask . . . . . . . . . . . : 255.255.248.0
Default Gateway . . . . . . . . . : 10.100.16.1
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
c:\>
The output result is:
c:\>for /F "tokens=1-2 delims=:" %a in ('ipconfig|find "Default"') do set ip=%b
c:\>set ip= 0.0.0.0
c:\>set ip=
c:\>set ip= 10.100.16.1
c:\>set ip=0.100.16.1
c:\>echo.
c:\>echo The Gateway is: 0.100.16.1
The Gateway is: 0.100.16.1
c:\>echo.
As the for loop iterates, it has the correct gateway address on the third iteration, but then a fourth iteration overwrites it with and incorrect value with the first digit truncated. I'm not sure why there is a fourth iteration since the word Default is present three times.
Why does this happen?
I'm trying to get this batch file to work. there are 2 ip ranges that need to be checked upon.
10.0.50.xxx
10.0.60.xxx
so far, I've done this with no avail.
[rolled back to original - OP has also experimented with * in the matching string]
ipconfig | find /i "IPv4 Address. . . . . . . . . . . : 10.0.50." >nul 2>nul && (
call script.bat
) || (
ipconfig | find /i "IPv4 Address. . . . . . . . . . . : 10.0.6." >nul 2>nul && (
call script2.bat
) || (
exit
any help would be much appreciated.
Is this suitable?
IPConfig|FindStr/IRC:"IPv4 .*: 10.0.50.">Nul 2>&1 && (Call script.bat) || (
IPConfig|FindStr/IRC:"IPv4 .*: 10.0.60.">Nul 2>&1 && Call script2.bat)
I'm afraid it is untested
I want to call msbuild from the function below and redirect the output to a new buffer.
My problem is that I need to use a variable, for the filename, and therefore cant use '!' (can I?), and when I use exe or system() read complains that I'm not giving it a proper file.
func! myFunction()
let findstr = "findstr /s /m " . '"' . expand("%:t") . '"' . " *.vcxproj"
for project in split(system(findstr), nr2char(10))
echo "Building '" . project . "'"
let msbuild = "c:\\windows\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe" . " " . project . " " . "/t:rebuild /p:configuration=debug"
:tabnew | r system(msbuild) "<--THIS LINE HERE
endfor
endfunc
The :read command takes a file not a vim expression. However it can read in from standard output via :read !{cmd}. Example :%r!ls. Using the :execute command you can build your new command with your variable.
exe '%r!' . msbuild
Or you can use :put along with the expression register if you want to use an expression like system(). (Probably want to follow this with :0d_ to delete the first empty line)
put=system(msbuild)
Now it looks like you are trying to build your project and get a list of errors. I would recommend you look into :make, the 'makeprg' option, and the quickfix list as this is a more vim way of building a project.
For more help see:
:h :r!
:h :exe
:h :pu
:h #=
:h :make
:h 'makeprg'
:h quickfix
This is a function you can use to execute arbitrary shell commands and present their output in a new window ( you can put this in your _vimrc):
let s:lastcmd = ''
function! s:RunShellCommand(cmdline, bang)
" Support for repeating last cmd with bang:
let _ = a:bang != '' ? s:lastcmd : a:cmdline == '' ? '' : join(map(split(a:cmdline), 'expand(v:val)'))
if _ == ''
return
endif
let s:lastcmd = _
let bufnr = bufnr('%')
let winnr = bufwinnr(_)
" You can position the new window whenever you want, I chose below + right:
silent! execute winnr < 0 ? 'belowright new ' . fnameescape(_) : winnr . 'wincmd w'
" I could set buftype=nofile, but then no switching back and forth buffers.
" The results are presented just for viewing, not editing, modify at will:
setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile wrap number
setlocal modifiable
silent! :%d
" Useful for debugging, if you encounter issues with fnameescape():
call setline(1, 'You entered: ' . a:cmdline)
call setline(2, 'Expanded to: ' . _)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !' . _
silent! execute 'autocmd BufUnload <buffer> execute bufwinnr(' . bufnr . ') . ''wincmd w'''
" If resizing is unwanted for commands with too much output, remove this line:
silent! execute 'autocmd BufEnter <buffer> execute ''resize '' . line(''$'')'
" You can use <localleader>r to re-execute the last command:
silent! execute 'nnoremap <silent> <buffer> <localleader>r :call <SID>RunShellCommand(''' . _ . ''', '''')<CR>'
execute 'resize ' . line('$')
setlocal nomodifiable
1
endfunction " RunShellCommand(cmdline)
command! -complete=shellcmd -nargs=* -bang Shell call s:RunShellCommand(<q-args>, '<bang>')
Use like:
:Shell gcc -ggdb -o test test.c && ./test
What I want to do is to copy the output as you would do with a manual copy-paste. That mostly means that unrecognised characters will be saved as ? or however they are displayed, and not as their char codes. Is there any way to do that?
If you want something as manual copy&paste, so using a sort of clipboard, than that is operating system dependent and it is not a bash question.
For example, on MacOS X you can:
echo $filename | pbcopy #pbcopy - save the output from echo to clipboard
find / -name 'pbpaste` #pbpaste - print the clipboard content
Sure here is something like this on Linux too. (xclip or so)
as Erik told you, if you do
some_command > outfile ; cat outfile
it is the same as
some_command #plain output to terminal.
in the file "outfile" you will get exactly the terminal output. (expect some rare cases)
and don't be confused with "less" or some other pagers, who really should change the unprintable characters to something else.
and lastly - if you have problems with codepage or so, try setup your environment variables like LANG, LC_ALL and LESSCHARSET (man less) and so on..
bash config
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
should help too in some cases. (man bash)
Try formulate you question more precise. :)
If you had examples of what you are trying to solve, I could be more specific.
Right now, I can only point you at
less -SR (show interpreting ANSI escapes)
ansifilter to convert ANSI escaped text to plain text (or HTML etc)
iconv, e.g.
.
cat myfile | iconv -f utf8 -t iso8859-1
Of course with that last one, substitute what ever character sets you have involved
Update
I just worked out the two most likely settings that will do what I think you are describing:
cat position | iconv -t latin1//TRANSLIT
cat position | iconv -t ASCII//TRANSLIT
If you have a UNICODE-heavy file position like this:
--> Applying move 131, ply 262: Q d3 - e2 + Checkmate .
... situation after:
⒏ ┊. . . . . . . .
⒎ ┊. . ♟ . . . . .
⒍ ┊. . ♙ . . . . .
⒌ ┊. . ♙ . ♙ . . ♟
⒋ ┊. . . . ♘ ♜ . ♚
⒊ ┊. . . ▫ . . . .
⒉ ┊. ♜ . . ♛ . . .
⒈ ┊. . . . ♔ . ♗ .
└────────────────
Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ
Will result in this:
--> Applying move 131, ply 262: Q d3 - e2 + Checkmate .
... situation after:
8. ?. . . . . . . .
7. ?. . ? . . . . .
6. ?. . ? . . . . .
5. ?. . ? . ? . . ?
4. ?. . . . ? ? . ?
3. ?. . . ? . . . .
2. ?. ? . . ? . . .
1. ?. . . . ? . ? .
+----------------
(A) (B) (C) (D) (E) (F) (G) (H)
ffmpeg extension is loaded as it is shown at phpinfo(), my file and script are at the same location, but I'm still getting this error.
Warning: Can't open movie file Untitled.avi in C:\xampp\htdocs\skelbiu\fetch.php on line 4
Fatal error: Call to a member function getDuration() on a non-object in C:\xampp\htdocs\skelbiu\fetch.php on line 5
My script:
extension_loaded('ffmpeg') or die('Error in loading ffmpeg');
$ffmpegInstance = new ffmpeg_movie('Untitled.avi');
echo "getDuration: " . $ffmpegInstance->getDuration() .
"getFrameCount: " . $ffmpegInstance->getFrameCount() .
"getFrameRate: " . $ffmpegInstance->getFrameRate() .
"getFilename: " . $ffmpegInstance->getFilename() .
"getComment: " . $ffmpegInstance->getComment() .
"getTitle: " . $ffmpegInstance->getTitle() .
"getAuthor: " . $ffmpegInstance->getAuthor() .
"getCopyright: " . $ffmpegInstance->getCopyright() .
"getArtist: " . $ffmpegInstance->getArtist() .
"getGenre: " . $ffmpegInstance->getGenre() .
"getTrackNumber: " . $ffmpegInstance->getTrackNumber() .
"getYear: " . $ffmpegInstance->getYear() .
"getFrameHeight: " . $ffmpegInstance->getFrameHeight() .
"getFrameWidth: " . $ffmpegInstance->getFrameWidth() .
"getPixelFormat: " . $ffmpegInstance->getPixelFormat() .
"getBitRate: " . $ffmpegInstance->getBitRate() .
"getVideoBitRate: " . $ffmpegInstance->getVideoBitRate() .
"getAudioBitRate: " . $ffmpegInstance->getAudioBitRate() .
"getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate() .
"getVideoCodec: " . $ffmpegInstance->getVideoCodec() .
"getAudioCodec: " . $ffmpegInstance->getAudioCodec() .
"getAudioChannels: " . $ffmpegInstance->getAudioChannels() .
"hasAudio: " . $ffmpegInstance->hasAudio();
I'm using php 5.2.9 (XAMPP 1.7.1), Windows 7.
Thanks in advance!
Your path to the movie has to be the full path. It doesn't matter where your script resides. Because you are using Win 7 and XAMPP, your path would be for example
$ffmpegInstance = new ffmpeg_movie('C:/xampp/htdocs/yourfolder/Untitled.avi');
I hope that this resolves your problem