shell脚本,awk常见初始化变量的题目。
文件 内容如下
clone=line1
gb=line1
gi=line1
lib=line1
gb=line2
gi=line2
lib=line2
clone=line3
gb=line3
gi=line3
lib=line3
gb=line4
gi=line4
lib=line4
如何变为下面的内容
lib=line1 gi=line1 gb=line1 clone=line1
lib=line2 gi=line2 gb=line2
lib=line3 gi=line3 gb=line3 clone=line3
lib=line4 gi=line4 gb=line4
[root@localhost 09-30]# cat file
clone=line1
gb=line1
gi=line1
lib=line1
gb=line2
gi=line2
lib=line2
clone=line3
gb=line3
gi=line3
lib=line3
gb=line4
gi=line4
lib=line4
[root@localhost 09-30]# awk '{a=$0" "a}/^lib/{print a;a=""}' file
lib=line1 gi=line1 gb=line1 clone=line1
lib=line2 gi=line2 gb=line2
lib=line3 gi=line3 gb=line3 clone=line3
lib=line4 gi=line4 gb=line4
[root@localhost 09-30]# awk '{a=$0" "a}/^lib/{print a}' file
lib=line1 gi=line1 gb=line1 clone=line1
lib=line2 gi=line2 gb=line2 lib=line1 gi=line1 gb=line1 clone=line1
lib=line3 gi=line3 gb=line3 clone=line3 lib=line2 gi=line2 gb=line2 lib=line1 gi=line1 gb=line1 clone=line1
lib=line4 gi=line4 gb=line4 lib=line3 gi=line3 gb=line3 clone=line3 lib=line2 gi=line2 gb=line2 lib=line1 gi=line1 gb=line1 clone=line1
[root@localhost 09-30]#
解题思路:awk '{a=$0" "a}/^lib/{print a;a=""}' file
上面的结果是这个地方输出的 {a=$0" "a},这个结果只是循环进行赋值,当碰到^lib开头才进行输出 a的值 ,print a;a="" 第一个a是上面循环输出的值,最后的a是把变量a重新初始化变量,为什么要初始化是因为下一次要用这个变量的时候,不能因为之前这个变量有值,影响下一次用,好让它继续下次的循环赋值。
shell脚本,awk常见初始化变量的题目。的更多相关文章
- shell脚本中常见的一些特殊符号和作用详解
这篇文章主要介绍了shell脚本中常见的一些特殊符号和它的作用详解,总结的很简洁,容易看懂,需要的朋友可以参考下 在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我 ...
- shell脚本中特定符合变量的含义
shell脚本中特定符合变量的含义: $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数.与位置变量不同,此选项参数可超过9个 $$ 脚本运行的当前进程PID号 ...
- shell脚本 awk工具
awk工具概述awk编程语言/数据处理引擎基于模式匹配检查输入文本,逐行处理并输出通常在shell脚本中,或取指定的数据单独用时,可对文本数据做统计 命令格式格式一:awk [选项] '[条件]{编辑 ...
- shell脚本中一些特殊变量
在shell脚本中,一些常见的特殊变量表示方式还是需要知道的 如下就是一些经常用到的特殊变量表示方法: $0 当前脚本名$1 $2... 传入脚本or函数的参数(大于10需大括号括起来)$ ...
- Shell脚本编写2------有关变量
shell脚本中变量定义方式十分简单,直接将值赋值给变量较好例如 :name="tuanzhang"注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.变量命名 ...
- shell脚本—基础知识,变量
shell脚本本质: 编译型语言 解释型语言 shell编程基本过程 1.建立shell文件 2.赋予shell文件执行权限,使用chmod命令修改权限 3.执行shell文件 shell变量: sh ...
- Bash基础——Shell脚本内部常用环境变量
$@和$*区别 不加引号的时候没区别 #! /usr/bin/bash function print_args_at { printf "%s\n" $@ echo $@ } fu ...
- shell脚本基础知识以及变量
一.基础知识 1.shell脚本的格式注意事项 第一行(一般必须写明):指定脚本使用的shell(若不写明也不影响脚本的执行,系统会自动以sh解析脚本)."#!/bin/bash" ...
- shell脚本 awk实现实时监控网卡流量
一.简介 通过第3方工具获得网卡流量,这个大家一定很清楚.其实通过脚本一样可以实现效果.下面是我个人工作中整理的数据.以下是shell脚本统计网卡流量. 现原理: cat /proc/net/dev ...
随机推荐
- 最近工作用到压缩,写一个zip压缩工具类
package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...
- Java | 基础归纳 | JPA
https://www.javacodegeeks.com/2015/04/jpa%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B.html JPA 全称====>Jav ...
- Verify the Developer App certificate for youraccount is trusted on your device
运行时报错-Verify the Developer App certificate for youraccountis trusted on your device. Open Settings ...
- DRF教程3-类视图
Rewriting our API using class-based views 使用类视图重写root views,只需要一点点重构. from django.http import Http40 ...
- Python标准库time
原文:http://www.cnblogs.com/qq78292959/archive/2013/03/22/2975786.html Python官方文档 在程序中,免不了和时间打交道,要学习ti ...
- python 基础(十四) 正则表达式
正则表达式 概念: 正则匹配就是一个模糊的匹配 只要符合我的匹配规则 就会认为是正确的数据(精确的匹配) 1.[] #代表原子表把想要匹配的内容写入原子表中 匹配包含的任意一位字符 [a] ...
- 利用arguments对象在javaScript中实现重载(overload)
一些概念: 重载(overload): 什么是: 相同函数名,不同参数列表的多个函数,在调用时,可根据传入参数的不同,自动选择对应的函数调用! 为什么: 减轻调用者的负担,一个函数名,可执行多种操作 ...
- @Results( 中 params 怎么用
http://blog.csdn.net/z69183787/article/details/16342553 struts2的@Result annotation 如何添加params,并且在页面取 ...
- nodejs 学习(2) 中间件
var connect=require('connect'), morgan=require('morgan'),//日志 bodyparser=require('body-parser'), ses ...
- 如何查找Oracle某列值相同的字段
相关的sql语句如下 select xm_guidfrom T_NZYDKgroup by xm_guidhaving count (*)>1